go/test/prove_constant_folding.go
Jorropo e1e056fa6a cmd/compile: fold constants found by prove
It is hit ~70k times building go.
This make the go binary, 0.04% smaller.
I didn't included benchmarks because this is just constant foldings
and is hard to mesure objectively.

For example, this enable rewriting things like:
  if x == 20 {
    return x + 30 + z
  }

Into:
  if x == 20 {
    return 50 + z
  }

It's not just fixing programer's code,
the ssa generator generate code like this sometimes.

Change-Id: I0861f342b27f7227b5f1c34d8267fa0057b1bbbc
GitHub-Last-Rev: 4c2f9b5216
GitHub-Pull-Request: golang/go#52669
Reviewed-on: https://go-review.googlesource.com/c/go/+/403735
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: David Chase <drchase@google.com>
2022-05-04 20:30:17 +00:00

32 lines
602 B
Go

// +build amd64
// errorcheck -0 -d=ssa/prove/debug=2
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func f0i(x int) int {
if x == 20 {
return x // ERROR "Proved.+is constant 20$"
}
if (x + 20) == 20 {
return x + 5 // ERROR "Proved.+is constant 0$"
}
return x / 2
}
func f0u(x uint) uint {
if x == 20 {
return x // ERROR "Proved.+is constant 20$"
}
if (x + 20) == 20 {
return x + 5 // ERROR "Proved.+is constant 0$"
}
return x / 2
}