1use rucc_base::float::{Float, Status};
82use rucc_ir::{Block, Def, Extra, Flags, Func, Imm, Inst, InstData, IntPred, Opcode, Type, Value};
83
84use crate::{Analyses, Analysis, Fuel, Pass, Preserved, Stats};
85
86const FOLDED: &str = "instruction with constant operands folded to a constant";
88
89const NO_FUEL: &str = "instruction not folded, the pass ran out of fuel";
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub struct Fold;
99
100impl Pass for Fold {
101 fn name(&self) -> &'static str {
102 "fold"
103 }
104
105 fn describe(&self) -> &'static str {
106 "an instruction whose operands are all constants becomes a constant"
107 }
108
109 fn preserves(&self) -> Preserved {
110 Preserved::ALL.without(Analysis::Liveness)
116 }
117
118 fn run(&self, func: &mut Func, _an: &mut Analyses, fuel: &mut Fuel) -> Stats {
119 fold_in(func, fuel)
120 }
121}
122
123pub(crate) fn fold_in(func: &mut Func, fuel: &mut Fuel) -> Stats {
129 let blocks: Vec<Block> = func.blocks().collect();
130 let mut stats = Stats::new();
131 for block in blocks {
132 let insts: Vec<Inst> = func.insts(block).collect();
133 for inst in insts {
134 let Some(folded) = evaluate(func, inst) else { continue };
135 if !fuel.take() {
136 stats.missed(NO_FUEL);
141 continue;
142 }
143 let ty = func[result_of(func, inst)].ty;
144 let at = func.add_imm(folded);
145 let data = &mut func[inst];
146 data.opcode = if ty.is_int() { Opcode::IConst } else { Opcode::FConst };
151 data.flags = Flags::NONE;
152 data.args = rucc_ir::ValueList::EMPTY;
153 data.extra = Extra::Imm(at);
154 stats.optimized(FOLDED);
155 }
156 }
157 stats
158}
159
160fn result_of(func: &Func, inst: Inst) -> Value {
162 func[inst].results().next().expect("an instruction that folds produces a value")
163}
164
165fn evaluate(func: &Func, inst: Inst) -> Option<Imm> {
170 let data = &func[inst];
171 if data.results != 1 {
172 return None;
173 }
174 let result = data.results().next()?;
175 let ty = func[result].ty;
176 if !ty.is_scalar() {
180 return None;
181 }
182 let args = &func[data.args];
183 match data.opcode {
187 Opcode::FNeg => return negated(func, *args.first()?, ty),
188 Opcode::Bitcast => return reinterpreted(func, *args.first()?, ty),
189 _ => {}
190 }
191 if !ty.is_int() {
192 return None;
193 }
194 match data.opcode {
195 Opcode::FPToSI | Opcode::FPToUI => {
196 let value = floating(func, *args.first()?)?;
197 to_integer(value, ty, data.opcode == Opcode::FPToSI)
198 }
199 _ => arithmetic(data, args, ty, &|value| constant(func, value)),
200 }
201}
202
203fn arithmetic(
211 data: &InstData,
212 args: &[Value],
213 ty: Type,
214 operand: &dyn Fn(Value) -> Option<(Imm, Type)>,
215) -> Option<Imm> {
216 match data.opcode {
217 Opcode::Trunc | Opcode::SExt | Opcode::ZExt => {
218 let (value, from) = operand(*args.first()?)?;
219 Some(convert(data.opcode, value, from, ty))
220 }
221 Opcode::Shl | Opcode::LShr | Opcode::AShr => {
222 let (value, from) = operand(*args.first()?)?;
223 let (count, count_ty) = operand(*args.get(1)?)?;
224 shift(data.opcode, value, from, count, count_ty, ty, data.flags)
225 }
226 Opcode::Add | Opcode::Sub | Opcode::Mul | Opcode::And | Opcode::Or | Opcode::Xor => {
227 let (lhs, lhs_ty) = operand(*args.first()?)?;
228 let (rhs, _) = operand(*args.get(1)?)?;
229 binary(data.opcode, lhs, rhs, lhs_ty, ty, data.flags)
230 }
231 Opcode::Ctlz | Opcode::Cttz | Opcode::Ctpop | Opcode::Bswap | Opcode::Bitreverse => {
232 let (value, from) = operand(*args.first()?)?;
233 count(data.opcode, value, from, ty)
234 }
235 Opcode::ICmp => {
236 let Extra::IntPred(pred) = data.extra else { return None };
237 let (lhs, from) = operand(*args.first()?)?;
238 let (rhs, _) = operand(*args.get(1)?)?;
239 Some(Imm::int(i128::from(compare(pred, lhs, rhs, from)), ty))
240 }
241 _ => None,
242 }
243}
244
245pub(crate) fn evaluated(func: &Func, value: Value, depth: u32) -> Option<(Imm, Type)> {
254 if let Some(found) = constant(func, value) {
255 return Some(found);
256 }
257 let next = depth.checked_sub(1)?;
258 let Def::Result { inst, .. } = func[value].def else { return None };
259 let data = &func[inst];
260 let ty = func[value].ty;
261 if data.results != 1 || !ty.is_int() || !ty.is_scalar() {
262 return None;
263 }
264 let found = arithmetic(data, &func[data.args], ty, &|arg| evaluated(func, arg, next))?;
265 Some((found, ty))
266}
267
268fn bits_of(func: &Func, value: Value) -> Option<u128> {
274 let Def::Result { inst, .. } = func[value].def else { return None };
275 let data = &func[inst];
276 if !matches!(data.opcode, Opcode::IConst | Opcode::FConst) {
277 return None;
278 }
279 let Extra::Imm(at) = data.extra else { return None };
280 Some(func[at].bits())
281}
282
283fn negated(func: &Func, operand: Value, ty: Type) -> Option<Imm> {
301 if !ty.is_float() {
302 return None;
303 }
304 let bits = bits_of(func, operand)?;
305 Some(Imm::from_bits(bits ^ 1u128 << (ty.bits() - 1)))
306}
307
308fn reinterpreted(func: &Func, operand: Value, ty: Type) -> Option<Imm> {
319 let from = func[operand].ty;
320 if !from.is_scalar() || from.bits() != ty.bits() {
321 return None;
322 }
323 let bits = bits_of(func, operand)?;
324 Some(Imm::from_bits(bits))
325}
326
327pub(crate) fn constant(func: &Func, value: Value) -> Option<(Imm, Type)> {
332 let Def::Result { inst, .. } = func[value].def else { return None };
333 if func[inst].opcode != Opcode::IConst {
334 return None;
335 }
336 let Extra::Imm(at) = func[inst].extra else { return None };
337 let ty = func[value].ty;
338 ty.is_int().then(|| (func[at], ty))
339}
340
341fn floating(func: &Func, value: Value) -> Option<Float> {
347 let Def::Result { inst, .. } = func[value].def else { return None };
348 if func[inst].opcode != Opcode::FConst {
349 return None;
350 }
351 let Extra::Imm(at) = func[inst].extra else { return None };
352 let format = func[value].ty.format()?.encoding();
353 Some(Float::from_bits(format, func[at].bits()))
354}
355
356fn to_integer(value: Float, to: Type, signed: bool) -> Option<Imm> {
365 let (number, status) = value.to_integer(to.bits(), signed);
366 (!status.has(Status::INVALID)).then(|| Imm::int(number, to))
367}
368
369fn convert(opcode: Opcode, value: Imm, from: Type, to: Type) -> Imm {
371 match opcode {
372 Opcode::Trunc | Opcode::SExt => Imm::int(value.signed(from), to),
375 _ => Imm::int(value.unsigned() as i128, to),
378 }
379}
380
381fn shift(
387 opcode: Opcode,
388 value: Imm,
389 from: Type,
390 count: Imm,
391 count_ty: Type,
392 to: Type,
393 flags: Flags,
394) -> Option<Imm> {
395 let by = count.unsigned();
396 if by >= u128::from(to.bits()) || count.signed(count_ty) < 0 {
397 return None;
398 }
399 let by = by as u32;
400 let exact = match opcode {
401 Opcode::Shl => value.signed(from).checked_shl(by)?,
402 Opcode::LShr => (value.unsigned() >> by) as i128,
406 _ => value.signed(from) >> by,
407 };
408 if opcode == Opcode::Shl && overflowed(exact, to, flags) {
409 return None;
410 }
411 Some(Imm::int(exact, to))
412}
413
414fn binary(opcode: Opcode, lhs: Imm, rhs: Imm, from: Type, to: Type, flags: Flags) -> Option<Imm> {
416 let (a, b) = (lhs.signed(from), rhs.signed(from));
417 let exact = match opcode {
418 Opcode::And => a & b,
421 Opcode::Or => a | b,
422 Opcode::Xor => a ^ b,
423 Opcode::Add => a.checked_add(b)?,
427 Opcode::Sub => a.checked_sub(b)?,
428 _ => a.checked_mul(b)?,
429 };
430 if overflowed(exact, to, flags) {
431 return None;
432 }
433 Some(Imm::int(exact, to))
434}
435
436pub(crate) fn compare(pred: IntPred, lhs: Imm, rhs: Imm, ty: Type) -> bool {
448 match pred {
449 IntPred::Eq => lhs == rhs,
450 IntPred::Ne => lhs != rhs,
451 IntPred::Slt => lhs.signed(ty) < rhs.signed(ty),
452 IntPred::Sle => lhs.signed(ty) <= rhs.signed(ty),
453 IntPred::Sgt => lhs.signed(ty) > rhs.signed(ty),
454 IntPred::Sge => lhs.signed(ty) >= rhs.signed(ty),
455 IntPred::Ult => lhs.unsigned() < rhs.unsigned(),
456 IntPred::Ule => lhs.unsigned() <= rhs.unsigned(),
457 IntPred::Ugt => lhs.unsigned() > rhs.unsigned(),
458 IntPred::Uge => lhs.unsigned() >= rhs.unsigned(),
459 }
460}
461
462fn count(opcode: Opcode, value: Imm, from: Type, to: Type) -> Option<Imm> {
479 let width = from.bits();
480 if width == 0 || width > 128 {
481 return None;
482 }
483 let spare = 128 - width;
487 let bits = value.unsigned();
488 let answer = match opcode {
489 Opcode::Ctpop => i128::from(bits.count_ones()),
490 Opcode::Ctlz => i128::from(bits.leading_zeros() - spare),
493 Opcode::Cttz => i128::from(bits.trailing_zeros().min(width)),
496 Opcode::Bswap if width % 8 == 0 => (bits.swap_bytes() >> spare) as i128,
497 Opcode::Bitreverse => (bits.reverse_bits() >> spare) as i128,
498 _ => return None,
499 };
500 Some(Imm::int(answer, to))
501}
502
503fn overflowed(exact: i128, to: Type, flags: Flags) -> bool {
508 let stored = Imm::int(exact, to);
509 if flags.contains(Flags::NSW) && stored.signed(to) != exact {
510 return true;
511 }
512 flags.contains(Flags::NUW) && (exact < 0 || stored.unsigned() != exact as u128)
513}
514
515#[cfg(test)]
516mod tests {
517 use rucc_base::Interner;
518 use rucc_base::float::Format;
519 use rucc_ir::{
520 Block, Builder, Extra, Flags, Float, Func, IntPred, Module, Opcode, Signature, Type, Value,
521 };
522 use rucc_target::{Arch, Env, Os, TargetInfo, Triple};
523
524 use crate::stats::Kind;
525 use crate::{Fuel, Pass, fold::Fold};
526
527 fn blank() -> (Interner, Func, Block) {
529 let mut names = Interner::new();
530 let name = names.intern("f");
531 let mut func = Func::new(name, Signature::new().with_returns(&[Type::int(64)]));
532 let block = func.create_block();
533 (names, func, block)
534 }
535
536 fn fold(func: &mut Func) -> bool {
539 Fold.run(func, &mut crate::machine::fixtures::analyses(), &mut Fuel::unlimited()).changed()
540 }
541
542 fn number(build: &mut Builder<'_>, text: &str, ty: Type) -> Value {
548 let format = ty.format().expect("a floating point type").encoding();
549 let (value, _) = super::Float::parse(text, format).expect("a number");
550 build.fconst(ty, value.to_bits())
551 }
552
553 fn value_of(func: &Func, value: Value, ty: Type) -> Option<i128> {
555 let rucc_ir::Def::Result { inst, .. } = func[value].def else { return None };
556 if func[inst].opcode != Opcode::IConst {
557 return None;
558 }
559 let Extra::Imm(at) = func[inst].extra else { return None };
560 Some(func[at].signed(ty))
561 }
562
563 fn float_bits(func: &Func, value: Value) -> Option<u128> {
565 let rucc_ir::Def::Result { inst, .. } = func[value].def else { return None };
566 if func[inst].opcode != Opcode::FConst {
567 return None;
568 }
569 let Extra::Imm(at) = func[inst].extra else { return None };
570 Some(func[at].bits())
571 }
572
573 #[test]
576 fn a_negated_floating_constant_becomes_a_constant() {
577 let (_, mut func, block) = blank();
578 let ty = Type::float(Float::F64);
579 let mut build = Builder::new(&mut func, block);
580 let one = number(&mut build, "1.0", ty);
581 let minus = build.unary(Opcode::FNeg, one, ty);
582 build.ret(&[minus]);
583 assert!(fold(&mut func));
584 assert_eq!(float_bits(&func, minus), Some(0xbff0_0000_0000_0000));
585 }
586
587 #[test]
591 fn a_negated_zero_keeps_its_sign_bit() {
592 let (_, mut func, block) = blank();
593 let ty = Type::float(Float::F64);
594 let mut build = Builder::new(&mut func, block);
595 let zero = number(&mut build, "0.0", ty);
596 let minus = build.unary(Opcode::FNeg, zero, ty);
597 build.ret(&[minus]);
598 assert!(fold(&mut func));
599 assert_eq!(float_bits(&func, minus), Some(1 << 63));
600 }
601
602 #[test]
606 fn a_negated_nan_keeps_its_payload() {
607 let (_, mut func, block) = blank();
608 let ty = Type::float(Float::F64);
609 let mut build = Builder::new(&mut func, block);
610 let nan = build.fconst(ty, 0x7ff8_0000_dead_beef);
611 let minus = build.unary(Opcode::FNeg, nan, ty);
612 build.ret(&[minus]);
613 assert!(fold(&mut func));
614 assert_eq!(float_bits(&func, minus), Some(0xfff8_0000_dead_beef));
615 }
616
617 #[test]
621 fn the_sign_bit_of_an_x87_value_is_the_top_bit_of_its_width() {
622 let (_, mut func, block) = blank();
623 let ty = Type::float(Float::F80);
624 let mut build = Builder::new(&mut func, block);
625 let one = number(&mut build, "1.0", ty);
626 let minus = build.unary(Opcode::FNeg, one, ty);
627 build.ret(&[minus]);
628 assert!(fold(&mut func));
629 let bits = float_bits(&func, minus).expect("a constant");
630 assert_eq!(bits >> 79 & 1, 1, "the sign bit is set");
631 assert_eq!(bits >> 80, 0, "nothing above the value is touched");
632 }
633
634 #[test]
638 fn a_bitcast_of_a_constant_is_the_same_bits() {
639 let (_, mut func, block) = blank();
640 let ty = Type::float(Float::F64);
641 let bits = Type::int(64);
642 let mut build = Builder::new(&mut func, block);
643 let value = number(&mut build, "-3.5", ty);
644 let number = build.unary(Opcode::Bitcast, value, bits);
645 let mask = build.iconst(bits, i128::from(i64::MAX));
646 let cleared = build.binary(Opcode::And, number, mask, Flags::NONE);
647 let back = build.unary(Opcode::Bitcast, cleared, ty);
648 build.ret(&[back]);
649 assert!(fold(&mut func));
650 assert_eq!(float_bits(&func, back), Some(0x400c_0000_0000_0000));
651 }
652
653 #[test]
656 fn a_bitcast_of_something_that_is_not_a_constant_is_left_alone() {
657 let mut names = Interner::new();
658 let name = names.intern("f");
659 let ty = Type::float(Float::F64);
660 let signature = Signature::new().with_params(&[ty]).with_returns(&[Type::int(64)]);
661 let mut func = Func::new(name, signature);
662 let block = func.create_block();
663 let x = func.append_param(block, ty);
664 let mut build = Builder::new(&mut func, block);
665 let number = build.unary(Opcode::Bitcast, x, Type::int(64));
666 build.ret(&[number]);
667 assert!(!fold(&mut func));
668 assert_eq!(value_of(&func, number, Type::int(64)), None);
669 }
670
671 #[test]
672 fn a_widened_constant_becomes_a_constant_of_the_wider_type() {
673 let (_, mut func, block) = blank();
674 let mut build = Builder::new(&mut func, block);
675 let narrow = build.iconst(Type::int(32), 7);
676 let wide = build.unary(Opcode::SExt, narrow, Type::int(64));
677 build.ret(&[wide]);
678 assert!(fold(&mut func));
679 assert_eq!(value_of(&func, wide, Type::int(64)), Some(7));
680 }
681
682 #[test]
683 fn sign_extension_copies_the_sign_and_zero_extension_does_not() {
684 for (opcode, expected) in [(Opcode::SExt, -1_i128), (Opcode::ZExt, 0xffff_ffff)] {
685 let (_, mut func, block) = blank();
686 let mut build = Builder::new(&mut func, block);
687 let narrow = build.iconst(Type::int(32), -1);
688 let wide = build.unary(opcode, narrow, Type::int(64));
689 build.ret(&[wide]);
690 assert!(fold(&mut func));
691 assert_eq!(value_of(&func, wide, Type::int(64)), Some(expected), "{opcode:?}");
692 }
693 }
694
695 #[test]
696 fn truncation_keeps_the_low_bits_and_reads_them_at_the_narrow_width() {
697 let (_, mut func, block) = blank();
698 let mut build = Builder::new(&mut func, block);
699 let wide = build.iconst(Type::int(32), 0x1234_5680);
700 let narrow = build.unary(Opcode::Trunc, wide, Type::int(8));
701 build.ret(&[narrow]);
702 assert!(fold(&mut func));
703 assert_eq!(value_of(&func, narrow, Type::int(8)), Some(-128));
704 }
705
706 #[test]
707 fn the_arithmetic_and_the_bitwise_operations_are_evaluated() {
708 let cases = [
709 (Opcode::Add, 6_i128, 7_i128, 13_i128),
710 (Opcode::Sub, 6, 7, -1),
711 (Opcode::Mul, 6, 7, 42),
712 (Opcode::And, 0b1100, 0b1010, 0b1000),
713 (Opcode::Or, 0b1100, 0b1010, 0b1110),
714 (Opcode::Xor, 0b1100, 0b1010, 0b0110),
715 ];
716 for (opcode, a, b, want) in cases {
717 let (_, mut func, block) = blank();
718 let mut build = Builder::new(&mut func, block);
719 let lhs = build.iconst(Type::int(64), a);
720 let rhs = build.iconst(Type::int(64), b);
721 let out = build.binary(opcode, lhs, rhs, Flags::NONE);
722 build.ret(&[out]);
723 assert!(fold(&mut func), "{opcode:?}");
724 assert_eq!(value_of(&func, out, Type::int(64)), Some(want), "{opcode:?}");
725 }
726 }
727
728 #[test]
729 fn the_three_shifts_are_evaluated_and_the_two_right_ones_differ_on_the_sign() {
730 let cases = [(Opcode::Shl, -8_i128, 1_i128, -16_i128), (Opcode::AShr, -8, 1, -4)];
731 for (opcode, a, b, want) in cases {
732 let (_, mut func, block) = blank();
733 let mut build = Builder::new(&mut func, block);
734 let lhs = build.iconst(Type::int(64), a);
735 let rhs = build.iconst(Type::int(64), b);
736 let out = build.binary(opcode, lhs, rhs, Flags::NONE);
737 build.ret(&[out]);
738 assert!(fold(&mut func), "{opcode:?}");
739 assert_eq!(value_of(&func, out, Type::int(64)), Some(want), "{opcode:?}");
740 }
741 let (_, mut func, block) = blank();
744 let mut build = Builder::new(&mut func, block);
745 let lhs = build.iconst(Type::int(64), -8);
746 let rhs = build.iconst(Type::int(64), 1);
747 let out = build.binary(Opcode::LShr, lhs, rhs, Flags::NONE);
748 build.ret(&[out]);
749 assert!(fold(&mut func));
750 assert_eq!(value_of(&func, out, Type::int(64)), Some(i128::from(i64::MAX) - 3));
751 }
752
753 fn one(opcode: Opcode, ty: Type, arg: i128) -> Option<i128> {
755 let (_, mut func, block) = blank();
756 let mut build = Builder::new(&mut func, block);
757 let value = build.iconst(ty, arg);
758 let out = build.unary(opcode, value, ty);
759 build.ret(&[out]);
760 fold(&mut func);
761 value_of(&func, out, ty)
762 }
763
764 #[test]
765 fn the_bit_counts_are_evaluated_at_the_width_they_were_asked_at() {
766 let cases = [
767 (Opcode::Ctlz, 64, 0x0000_1000_0000_0000_i128, 19_i128),
768 (Opcode::Ctlz, 32, 0x0000_1000, 19),
769 (Opcode::Cttz, 64, 0x0000_1000_0000_0000, 44),
770 (Opcode::Cttz, 32, 0x0000_1000, 12),
771 (Opcode::Ctpop, 64, 0x0000_1000_0000_0000, 1),
772 (Opcode::Ctpop, 32, -1, 32),
773 (Opcode::Ctpop, 64, -1, 64),
774 ];
775 for (opcode, width, arg, want) in cases {
776 let ty = Type::int(width);
777 assert_eq!(one(opcode, ty, arg), Some(want), "{opcode:?} at {width} of {arg:#x}");
778 }
779 }
780
781 #[test]
782 fn a_search_for_a_bit_in_a_zero_answers_the_width_the_expansion_answers() {
783 for width in [8_u32, 16, 32, 64] {
784 let ty = Type::int(width);
785 let want = Some(i128::from(width));
786 assert_eq!(one(Opcode::Ctlz, ty, 0), want, "leading, at {width}");
787 assert_eq!(one(Opcode::Cttz, ty, 0), want, "trailing, at {width}");
788 assert_eq!(one(Opcode::Ctpop, ty, 0), Some(0), "count, at {width}");
789 }
790 }
791
792 #[test]
793 fn the_two_reversals_are_evaluated_and_a_byte_swap_of_a_part_of_a_byte_is_not() {
794 let ty = Type::int(32);
795 assert_eq!(one(Opcode::Bswap, ty, 0x1234_5678), Some(0x7856_3412));
796 assert_eq!(one(Opcode::Bswap, Type::int(16), 0x1234), Some(0x3412));
797 assert_eq!(one(Opcode::Bitreverse, Type::int(8), 0b1010_1100), Some(0b0011_0101));
798 let (_, mut func, block) = blank();
801 let mut build = Builder::new(&mut func, block);
802 let value = build.iconst(Type::int(4), 0b1010);
803 let out = build.unary(Opcode::Bswap, value, Type::int(4));
804 build.ret(&[out]);
805 assert!(!fold(&mut func));
806 }
807
808 #[test]
809 fn a_comparison_of_two_constants_becomes_a_one_or_a_nought() {
810 let cases = [
811 (IntPred::Eq, 7_i128, 7_i128, true),
812 (IntPred::Eq, 7, 8, false),
813 (IntPred::Ne, 7, 8, true),
814 (IntPred::Slt, -1, 1, true),
815 (IntPred::Sle, -1, -1, true),
816 (IntPred::Sgt, -1, 1, false),
817 (IntPred::Sge, 1, -1, true),
818 (IntPred::Ult, -1, 1, false),
821 (IntPred::Ule, -1, 1, false),
822 (IntPred::Ugt, -1, 1, true),
823 (IntPred::Uge, -1, 1, true),
824 ];
825 for (pred, a, b, want) in cases {
826 let (_, mut func, block) = blank();
827 let mut build = Builder::new(&mut func, block);
828 let lhs = build.iconst(Type::int(64), a);
829 let rhs = build.iconst(Type::int(64), b);
830 let out = build.icmp(pred, lhs, rhs);
831 build.ret(&[out]);
832 assert!(fold(&mut func), "{pred:?} {a} {b}");
833 let got = value_of(&func, out, Type::I1).expect("the comparison folded");
836 assert_eq!(got != 0, want, "{pred:?} {a} {b}");
837 }
838 }
839
840 #[test]
841 fn a_comparison_at_a_narrow_width_is_read_at_that_width() {
842 let ty = Type::int(8);
845 for (pred, want) in [(IntPred::Slt, true), (IntPred::Ult, false)] {
846 let (_, mut func, block) = blank();
847 let mut build = Builder::new(&mut func, block);
848 let lhs = build.iconst(ty, 255);
849 let rhs = build.iconst(ty, 1);
850 let out = build.icmp(pred, lhs, rhs);
851 build.ret(&[out]);
852 assert!(fold(&mut func), "{pred:?}");
853 let got = value_of(&func, out, Type::I1).expect("the comparison folded");
854 assert_eq!(got != 0, want, "{pred:?}");
855 }
856 }
857
858 #[test]
859 fn a_comparison_with_one_constant_operand_is_left_alone() {
860 let (_, mut func, block) = blank();
861 let ty = Type::int(64);
862 let param = func.append_param(block, ty);
863 let mut build = Builder::new(&mut func, block);
864 let rhs = build.iconst(ty, 3);
865 let out = build.icmp(IntPred::Eq, param, rhs);
866 build.ret(&[out]);
867 assert!(!fold(&mut func));
868 }
869
870 #[test]
871 fn a_bit_count_of_something_that_is_not_a_constant_is_left_alone() {
872 for opcode in [Opcode::Ctlz, Opcode::Cttz, Opcode::Ctpop, Opcode::Bswap] {
873 let (_, mut func, block) = blank();
874 let ty = Type::int(64);
875 let param = func.append_param(block, ty);
876 let mut build = Builder::new(&mut func, block);
877 let out = build.unary(opcode, param, ty);
878 build.ret(&[out]);
879 assert!(!fold(&mut func), "{opcode:?}");
880 }
881 }
882
883 #[test]
884 fn a_shift_by_the_width_or_more_is_left_alone_because_the_language_does_not_define_it() {
885 for count in [64_i128, 65, -1] {
886 let (_, mut func, block) = blank();
887 let mut build = Builder::new(&mut func, block);
888 let lhs = build.iconst(Type::int(64), 1);
889 let rhs = build.iconst(Type::int(64), count);
890 let out = build.binary(Opcode::Shl, lhs, rhs, Flags::NONE);
891 build.ret(&[out]);
892 assert!(!fold(&mut func), "a shift by {count} was folded");
893 }
894 }
895
896 #[test]
897 fn an_operation_that_wraps_folds_and_the_same_one_promising_it_will_not_does_not() {
898 let big = i128::from(i32::MAX);
899 for (flags, folds) in [(Flags::NONE, true), (Flags::NSW, false)] {
900 let (_, mut func, block) = blank();
901 let mut build = Builder::new(&mut func, block);
902 let lhs = build.iconst(Type::int(32), big);
903 let rhs = build.iconst(Type::int(32), 1);
904 let out = build.binary(Opcode::Add, lhs, rhs, flags);
905 build.ret(&[out]);
906 assert_eq!(fold(&mut func), folds, "{flags}");
907 if folds {
908 assert_eq!(value_of(&func, out, Type::int(32)), Some(i128::from(i32::MIN)));
909 }
910 }
911 }
912
913 #[test]
914 fn an_unsigned_promise_is_broken_by_a_negative_result_as_well_as_by_a_large_one() {
915 let (_, mut func, block) = blank();
916 let mut build = Builder::new(&mut func, block);
917 let lhs = build.iconst(Type::int(32), 1);
918 let rhs = build.iconst(Type::int(32), 2);
919 let out = build.binary(Opcode::Sub, lhs, rhs, Flags::NUW);
920 build.ret(&[out]);
921 assert!(!fold(&mut func));
922 }
923
924 #[test]
925 fn an_operation_with_one_constant_operand_is_left_alone() {
926 let (_, mut func, block) = blank();
927 let param = func.append_param(block, Type::int(64));
928 let mut build = Builder::new(&mut func, block);
929 let rhs = build.iconst(Type::int(64), 7);
930 let out = build.binary(Opcode::Add, param, rhs, Flags::NONE);
931 build.ret(&[out]);
932 assert!(!fold(&mut func));
933 assert_eq!(func[out_inst(&func, out)].opcode, Opcode::Add);
934 }
935
936 #[test]
937 fn a_conversion_to_an_integer_truncates_toward_zero() {
938 for (text, expected) in [("2.75", 2_i128), ("-2.75", -2), ("0.5", 0), ("-0.5", 0)] {
939 let (_, mut func, block) = blank();
940 let mut build = Builder::new(&mut func, block);
941 let value = number(&mut build, text, Type::float(Float::F64));
942 let out = build.unary(Opcode::FPToSI, value, Type::int(32));
943 build.ret(&[out]);
944 assert!(fold(&mut func), "{text}");
945 assert_eq!(value_of(&func, out, Type::int(32)), Some(expected), "{text}");
946 }
947 }
948
949 #[test]
950 fn a_negative_number_converts_to_an_unsigned_type_only_when_truncating_lands_on_zero() {
951 for (text, expected) in [("-0.5", Some(0)), ("-1.5", None)] {
952 let (_, mut func, block) = blank();
953 let mut build = Builder::new(&mut func, block);
954 let value = number(&mut build, text, Type::float(Float::F64));
955 let out = build.unary(Opcode::FPToUI, value, Type::int(32));
956 build.ret(&[out]);
957 assert_eq!(fold(&mut func), expected.is_some(), "{text}");
958 assert_eq!(value_of(&func, out, Type::int(32)), expected, "{text}");
959 }
960 }
961
962 #[test]
963 fn a_number_the_destination_type_has_no_room_for_is_left_alone() {
964 let (_, mut func, block) = blank();
965 let mut build = Builder::new(&mut func, block);
966 let value = number(&mut build, "1e30", Type::float(Float::F64));
967 let out = build.unary(Opcode::FPToSI, value, Type::int(32));
968 build.ret(&[out]);
969 assert!(!fold(&mut func));
970 assert_eq!(func[out_inst(&func, out)].opcode, Opcode::FPToSI);
971 }
972
973 #[test]
974 fn a_nan_is_left_alone() {
975 let (_, mut func, block) = blank();
976 let mut build = Builder::new(&mut func, block);
977 let value = build.fconst(Type::float(Float::F64), 0x7ff8_0000_0000_0000);
978 let out = build.unary(Opcode::FPToSI, value, Type::int(32));
979 build.ret(&[out]);
980 assert!(!fold(&mut func));
981 }
982
983 #[test]
984 fn a_constant_is_read_in_the_format_its_own_type_gives_it() {
985 let bits = super::Float::parse("3.0", Format::X87Extended).expect("a number").0.to_bits();
988 for (float, expected) in [(Float::F80, 3_i128), (Float::F128, 0)] {
989 let (_, mut func, block) = blank();
990 let mut build = Builder::new(&mut func, block);
991 let value = build.fconst(Type::float(float), bits);
992 let out = build.unary(Opcode::FPToSI, value, Type::int(32));
993 build.ret(&[out]);
994 assert!(fold(&mut func), "{float}");
995 assert_eq!(value_of(&func, out, Type::int(32)), Some(expected), "{float}");
996 }
997 }
998
999 #[test]
1000 fn a_conversion_of_something_that_is_not_a_constant_is_left_alone() {
1001 let (_, mut func, block) = blank();
1002 let param = func.append_param(block, Type::float(Float::F64));
1003 let mut build = Builder::new(&mut func, block);
1004 let out = build.unary(Opcode::FPToSI, param, Type::int(32));
1005 build.ret(&[out]);
1006 assert!(!fold(&mut func));
1007 }
1008
1009 #[test]
1010 fn a_divide_is_not_folded_even_when_both_operands_are_constants() {
1011 for opcode in [Opcode::SDiv, Opcode::UDiv, Opcode::SRem, Opcode::URem] {
1012 let (_, mut func, block) = blank();
1013 let mut build = Builder::new(&mut func, block);
1014 let lhs = build.iconst(Type::int(64), 42);
1015 let rhs = build.iconst(Type::int(64), 7);
1016 let out = build.binary(opcode, lhs, rhs, Flags::NONE);
1017 build.ret(&[out]);
1018 assert!(!fold(&mut func), "{opcode:?}");
1019 }
1020 }
1021
1022 #[test]
1023 fn folding_leaves_the_function_something_the_verifier_accepts() {
1024 let mut names = Interner::new();
1025 let name = names.intern("f");
1026 let mut func = Func::new(name, Signature::new().with_returns(&[Type::int(64)]));
1027 let block = func.create_block();
1028 let mut build = Builder::new(&mut func, block);
1029 let narrow = build.iconst(Type::int(32), 7);
1030 let wide = build.unary(Opcode::SExt, narrow, Type::int(64));
1031 build.ret(&[wide]);
1032 assert!(fold(&mut func));
1033 let target = TargetInfo::new(Triple::new(Arch::X86_64, Os::Linux, Env::Gnu));
1034 let module_name = names.intern("m");
1035 let mut module = Module::new(module_name, &target);
1036 module.add_func(func);
1037 rucc_ir::verify(&module, &names).expect("folding does not break the IR");
1038 }
1039
1040 #[test]
1041 fn fuel_stops_the_transformation_and_not_the_walk() {
1042 let build_two = |func: &mut Func, block: Block| {
1043 let mut build = Builder::new(func, block);
1044 let a = build.iconst(Type::int(32), 7);
1045 let wide_a = build.unary(Opcode::SExt, a, Type::int(64));
1046 let b = build.iconst(Type::int(32), 9);
1047 let wide_b = build.unary(Opcode::SExt, b, Type::int(64));
1048 let sum = build.binary(Opcode::Add, wide_a, wide_b, Flags::NONE);
1049 build.ret(&[sum]);
1050 (wide_a, wide_b)
1051 };
1052
1053 let (_, mut none, block) = blank();
1054 let (first, _) = build_two(&mut none, block);
1055 let stats =
1056 Fold.run(&mut none, &mut crate::machine::fixtures::analyses(), &mut Fuel::of(0));
1057 assert!(!stats.changed());
1058 assert_eq!(none[out_inst(&none, first)].opcode, Opcode::SExt);
1059 assert_eq!(stats.count(Kind::Missed, super::NO_FUEL), 2);
1062
1063 let (_, mut one, block) = blank();
1064 let (first, second) = build_two(&mut one, block);
1065 let mut fuel = Fuel::of(1);
1066 let stats = Fold.run(&mut one, &mut crate::machine::fixtures::analyses(), &mut fuel);
1067 assert!(stats.changed());
1068 assert_eq!(fuel.spent(), 1);
1069 assert_eq!(stats.count(Kind::Optimized, super::FOLDED), 1);
1070 assert_eq!(stats.count(Kind::Missed, super::NO_FUEL), 1);
1071 assert_eq!(one[out_inst(&one, first)].opcode, Opcode::IConst);
1072 assert_eq!(one[out_inst(&one, second)].opcode, Opcode::SExt);
1073 }
1074
1075 #[test]
1076 fn folding_one_operation_uncovers_the_next() {
1077 let (_, mut func, block) = blank();
1078 let mut build = Builder::new(&mut func, block);
1079 let a = build.iconst(Type::int(32), 7);
1080 let wide = build.unary(Opcode::SExt, a, Type::int(64));
1081 let b = build.iconst(Type::int(64), 9);
1082 let sum = build.binary(Opcode::Add, wide, b, Flags::NONE);
1083 build.ret(&[sum]);
1084 assert!(fold(&mut func));
1085 assert_eq!(value_of(&func, sum, Type::int(64)), Some(16));
1088 }
1089
1090 #[test]
1091 fn a_constant_is_left_where_it_is_and_folding_it_again_changes_nothing() {
1092 let (_, mut func, block) = blank();
1093 let mut build = Builder::new(&mut func, block);
1094 let a = build.iconst(Type::int(32), 7);
1095 let wide = build.unary(Opcode::SExt, a, Type::int(64));
1096 build.ret(&[wide]);
1097 assert!(fold(&mut func));
1098 assert!(!fold(&mut func), "a second run found something to do");
1099 }
1100
1101 fn out_inst(func: &Func, value: Value) -> rucc_ir::Inst {
1103 match func[value].def {
1104 rucc_ir::Def::Result { inst, .. } => inst,
1105 rucc_ir::Def::Param { .. } => panic!("a parameter has no instruction"),
1106 }
1107 }
1108}