compiler: do not emit nil checks for loading closure variables

Closure variables are allocated in a parent function and are thus never
nil. Don't do a nil check before reading or modifying the value.

This commit results in a slight reduction in code size in some test
cases: calls.go, channel.go, goroutines.go, json.go, sort.go -
presumably wherever closures are used.
This commit is contained in:
Ayke van Laethem
2021-05-25 23:39:28 +02:00
committed by Ron Evans
parent ec325c0643
commit 3edcdb5f0d
5 changed files with 14 additions and 21 deletions
+4
View File
@@ -162,6 +162,10 @@ func (b *builder) createNilCheck(inst ssa.Value, ptr llvm.Value, blockPrefix str
case *ssa.Alloc:
// An alloc is never nil.
return
case *ssa.FreeVar:
// A free variable is allocated in a parent function and is thus never
// nil.
return
case *ssa.IndexAddr:
// This pointer is the result of an index operation into a slice or
// array. Such slices/arrays are already bounds checked so the pointer
+2
View File
@@ -994,6 +994,8 @@ func getPos(val posser) token.Pos {
switch val := val.(type) {
case *ssa.MakeInterface:
return getPos(val.X)
case *ssa.MakeClosure:
return val.Fn.(*ssa.Function).Pos()
case *ssa.Return:
syntax := val.Parent().Syntax()
if syntax != nil {
-10
View File
@@ -86,14 +86,6 @@ entry:
define hidden void @"main.closureFunctionGoroutine$1"(i32 %x, i8* %context, i8* %parentHandle) unnamed_addr {
entry:
%0 = icmp eq i8* %context, null
br i1 %0, label %store.throw, label %store.next
store.throw: ; preds = %entry
call void @runtime.nilPanic(i8* undef, i8* null)
unreachable
store.next: ; preds = %entry
%unpack.ptr = bitcast i8* %context to i32*
store i32 7, i32* %unpack.ptr, align 4
ret void
@@ -112,8 +104,6 @@ entry:
declare void @runtime.printint32(i32, i8*, i8*)
declare void @runtime.nilPanic(i8*, i8*)
define hidden void @main.funcGoroutine(i8* %fn.context, void (i32, i8*, i8*)* %fn.funcptr, i8* %context, i8* %parentHandle) unnamed_addr {
entry:
%0 = call i8* @runtime.alloc(i32 12, i8* undef, i8* null)
-10
View File
@@ -62,14 +62,6 @@ entry:
define hidden void @"main.closureFunctionGoroutine$1"(i32 %x, i8* %context, i8* %parentHandle) unnamed_addr {
entry:
%0 = icmp eq i8* %context, null
br i1 %0, label %store.throw, label %store.next
store.throw: ; preds = %entry
call void @runtime.nilPanic(i8* undef, i8* null)
unreachable
store.next: ; preds = %entry
%unpack.ptr = bitcast i8* %context to i32*
store i32 7, i32* %unpack.ptr, align 4
ret void
@@ -77,8 +69,6 @@ store.next: ; preds = %entry
declare void @runtime.printint32(i32, i8*, i8*)
declare void @runtime.nilPanic(i8*, i8*)
define hidden void @main.funcGoroutine(i8* %fn.context, i32 %fn.funcptr, i8* %context, i8* %parentHandle) unnamed_addr {
entry:
%0 = call i32 @runtime.getFuncPtr(i8* %fn.context, i32 %fn.funcptr, i8* nonnull @"reflect/types.funcid:func:{basic:int}{}", i8* undef, i8* null)