Rollup merge of #74491 - xldenis:constant-binop-opt, r=oli-obk

Optimize away BitAnd and BitOr when possible

This PR lets `const_prop` optimize away `a | true == true` , `a & false == false` and `a * 0 = 0`. While I was writing this I've realized that constant propagation misses a lot of opportunities. For example:  https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2a4b45e772f214210a36749b27223bb0

Constant propagation doesn't seem to... propagate constants, additionally the way constant propagation is currently setup makes it tricky to add cases like `a | false == a`.

I tried to organize `eval_rvalue_with_identities` to make the pattern of the optimizations easier to see but it still obscurs what should be a simple peephole optmization.

cc @oli-obk
This commit is contained in:
Manish Goregaokar 2020-07-24 10:01:32 -07:00 committed by GitHub
commit e59effed30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 187 additions and 24 deletions

View file

@ -1,7 +1,7 @@
// build-fail
// Regression test for #66975
#![warn(const_err)]
#![warn(const_err, unconditional_panic)]
#![feature(never_type)]
struct PrintName<T>(T);
@ -9,6 +9,7 @@ struct PrintName<T>(T);
impl<T> PrintName<T> {
const VOID: ! = { let x = 0 * std::mem::size_of::<T>(); [][x] };
//~^ WARN any use of this value will cause an error
}
fn f<T>() {

View file

@ -9,11 +9,11 @@ LL | const VOID: ! = { let x = 0 * std::mem::size_of::<T>(); [][x] };
note: the lint level is defined here
--> $DIR/index-out-of-bounds-never-type.rs:4:9
|
LL | #![warn(const_err)]
LL | #![warn(const_err, unconditional_panic)]
| ^^^^^^^^^
error: erroneous constant encountered
--> $DIR/index-out-of-bounds-never-type.rs:15:13
--> $DIR/index-out-of-bounds-never-type.rs:16:13
|
LL | let _ = PrintName::<T>::VOID;
| ^^^^^^^^^^^^^^^^^^^^