Optimize/eliminate bounds checking

TODO: do better at it by tracking min/max values of integers. The
following straightforward code doesn't have its bounds checks removed:

    for _, n := range slice {
        println(n)
    }
This commit is contained in:
Ayke van Laethem
2018-09-02 16:24:50 +02:00
parent 42cddd3260
commit 88b6b2e7f5
7 changed files with 48 additions and 19 deletions
+11 -2
View File
@@ -35,8 +35,9 @@ type Program struct {
type Function struct {
fn *ssa.Function
llvmFn llvm.Value
linkName string
blocking bool
linkName string // go:linkname pragma
nobounds bool // go:nobounds pragma
blocking bool // calculated by AnalyseBlockingRecursive
flag bool // used by dead code elimination
addressTaken bool // used as function pointer, calculated by AnalyseFunctionPointers
parents []*Function // calculated by AnalyseCallgraph
@@ -169,6 +170,14 @@ func (f *Function) parsePragmas() {
if hasUnsafeImport(f.fn.Pkg.Pkg) {
f.linkName = parts[2]
}
case "//go:nobounds":
// Skip bounds checking in this function. Useful for some
// runtime functions.
// This is somewhat dangerous and thus only imported in packages
// that import unsafe.
if hasUnsafeImport(f.fn.Pkg.Pkg) {
f.nobounds = true
}
}
}
}