Skip to content

Commit

Permalink
feat: simplify all unsigned constant NOT instructions (#4230)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR expands the existing simplification of constant NOT instructions
to cover all unsigned integers.


## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Feb 5, 2024
1 parent b3ddf10 commit fab4a6e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,10 @@ impl Instruction {
// Limit optimizing ! on constants to only booleans. If we tried it on fields,
// there is no Not on FieldElement, so we'd need to convert between u128. This
// would be incorrect however since the extra bits on the field would not be flipped.
Value::NumericConstant { constant, typ } if *typ == Type::bool() => {
let value = constant.is_zero() as u128;
SimplifiedTo(dfg.make_constant(value.into(), Type::bool()))
Value::NumericConstant { constant, typ } if typ.is_unsigned() => {
// As we're casting to a `u128`, we need to clear out any upper bits that the NOT fills.
let value = !constant.to_u128() % (1 << typ.bit_size());
SimplifiedTo(dfg.make_constant(value.into(), typ.clone()))
}
Value::Instruction { instruction, .. } => {
// !!v => v
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "literal_not_simplification"
type = "bin"
authors = [""]
compiler_version = ">=0.23.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let four: u8 = 4;
let not_four: u8 = !four;

let five: u8 = 5;
let not_five: u8 = !five;
assert(not_four != not_five);
}

0 comments on commit fab4a6e

Please sign in to comment.