1
0
mirror of https://github.com/golang/go synced 2024-10-04 16:21:22 -06:00
go/src/cmd/gc
Russ Cox ca9975a45e cmd/gc: handle non-escaping address-taken variables better
This CL makes the bitmaps a little more precise about variables
that have their address taken but for which the address does not
escape to the heap, so that the variables are kept in the stack frame
rather than allocated on the heap.

The code before this CL handled these variables by treating every
return statement as using every such variable and depending on
liveness analysis to essentially treat the variable as live during the
entire function. That approach has false positives and (worse) false
negatives. That is, it's both sloppy and buggy:

        func f(b1, b2 bool) {	// x live here! (sloppy)
                if b2 {
                        print(0) // x live here! (sloppy)
                        return
                }
                var z **int
                x := new(int)
                *x = 42
                z = &x
                print(**z) // x live here (conservative)
                if b2 {
                        print(1) // x live here (conservative)
                        return
                }
                for {
                        print(**z) // x not live here (buggy)
                }
        }

The first two liveness annotations (marked sloppy) are clearly
wrong: x cannot be live if it has not yet been declared.

The last liveness annotation (marked buggy) is also wrong:
x is live here as *z, but because there is no return statement
reachable from this point in the code, the analysis treats x as dead.

This CL changes the liveness calculation to mark such variables
live exactly at points in the code reachable from the variable
declaration. This keeps the conservative decisions but fixes
the sloppy and buggy ones.

The CL also detects ambiguously live variables, those that are
being marked live but may not actually have been initialized,
such as in this example:

        func f(b1 bool) {
                var z **int
                if b1 {
                        x := new(int)
                        *x = 42
                        z = &x
                } else {
                        y := new(int)
                        *y = 54
                        z = &y
                }
                print(**z) // x, y live here (conservative)
        }

Since the print statement is reachable from the declaration of x,
x must conservatively be marked live. The same goes for y.
Although both x and y are marked live at the print statement,
clearly only one of them has been initialized. They are both
"ambiguously live".

These ambiguously live variables cause problems for garbage
collection: the collector cannot ignore them but also cannot
depend on them to be initialized to valid pointer values.

Ambiguously live variables do not come up too often in real code,
but recent changes to the way map and interface runtime functions
are invoked has created a large number of ambiguously live
compiler-generated temporary variables. The next CL will adjust
the analysis to understand these temporaries better, to make
ambiguously live variables fairly rare.

Once ambiguously live variables are rare enough, another CL will
introduce code at the beginning of a function to zero those
slots on the stack. At that point the garbage collector and the
stack copying routines will be able to depend on the guarantee that
if a slot is marked as live in a liveness bitmap, it is initialized.

R=khr
CC=golang-codereviews, iant
https://golang.org/cl/51810043
2014-01-16 10:32:30 -05:00
..
align.c cmd/gc: squelch spurious "invalid recursive type" error 2013-09-09 13:03:59 -04:00
array.c cmd/5g, cmd/5l, cmd/6g, cmd/6l, cmd/8g, cmd/8l, cmd/gc, runtime: generate pointer maps by liveness analysis 2013-12-05 17:35:22 -08:00
bisonerrors cmd/gc: make bisonerrors compatible with GNU Bison 3.0 2013-07-30 04:31:15 +02:00
bits.c gc: format nits 2011-11-07 11:42:08 -05:00
builtin.c runtime, gc: call interface conversion routines by reference. 2013-12-17 16:55:06 -08:00
bv.c cmd/gc: handle non-escaping address-taken variables better 2014-01-16 10:32:30 -05:00
closure.c cmd/gc: fix method values whose receiver is an unnamed interface. 2013-08-29 10:00:58 +02:00
const.c cmd/gc: remove mentions of "ideal" from error messages. 2013-08-16 12:40:02 +10:00
cplx.c cmd/gc: silence valgrind error 2013-03-13 16:12:38 -04:00
dcl.c cmd/5g, cmd/6g, cmd/8g: use liblink 2013-12-08 22:51:55 -05:00
doc.go cmd/godoc: use go/build to determine package and example files 2013-02-19 11:19:58 -08:00
esc.c cmd/gc: support x[i:j:k] 2013-07-01 20:32:36 -04:00
export.c cmd/gc: implement -pack flag 2013-12-17 21:43:33 -05:00
fmt.c cmd/gc: mark OGOTO as a statement for formatters. 2014-01-10 01:33:24 +01:00
gen.c cmd/gc: return canonical Node* from temp 2014-01-14 10:43:13 -05:00
go.errors cmd/gc: better error messages for C-style if statements. 2013-08-19 11:49:59 +10:00
go.h cmd/gc: handle non-escaping address-taken variables better 2014-01-16 10:32:30 -05:00
go.y cmd/gc: qualified embedded fields with owner package. 2013-10-02 12:27:33 -04:00
init.c cmd/gc: do not generate code for var _ = ... unless necessary 2012-12-30 12:01:53 -05:00
inl.c cmd/gc: save local var list before inlining 2013-06-11 20:23:21 -07:00
lex.c cmd/gc: add -live flag for debugging liveness maps 2014-01-14 10:40:16 -05:00
Makefile build: update Makefile to track source code dependencies better 2012-03-13 03:31:11 +08:00
md5.c build: remove various uses of C undefined behavior 2013-09-09 15:07:23 -04:00
md5.h
mkbuiltin cmd/gc: update runtime.go for new map implementation. 2013-03-27 21:51:07 +01:00
mkbuiltin1.c cmd/gc: silence clang warning 2013-10-29 11:50:18 -04:00
mkopnames gc: use octal escapes in mkopnames 2012-01-31 18:15:42 -08:00
mparith1.c cmd/gc: fix comparison order of parameters in mpcmpfltc(a, b) 2013-12-16 16:54:10 -05:00
mparith2.c build: remove various uses of C undefined behavior 2013-09-09 15:07:23 -04:00
mparith3.c gc: make constant arith errors a little more friendly 2012-02-11 00:50:56 -05:00
obj.c cmd/gc: use 100x less memory for []byte("string") 2014-01-06 20:43:44 -05:00
order.c cmd/gc: move genembedtramp into portable code 2013-06-11 09:41:49 -04:00
pgen.c cmd/gc: handle non-escaping address-taken variables better 2014-01-16 10:32:30 -05:00
plive.c cmd/gc: handle non-escaping address-taken variables better 2014-01-16 10:32:30 -05:00
popt.c cmd/5g, cmd/6g, cmd/8g: use liblink 2013-12-08 22:51:55 -05:00
popt.h cmd/gc: eliminate redundant &x.Field nil checks 2013-09-17 16:54:22 -04:00
racewalk.c cmd/gc: inline copy in frontend to call memmove directly. 2013-09-12 00:15:28 +02:00
range.c runtime: pass key/value to map accessors by reference, not by value. 2013-12-02 13:05:04 -08:00
reflect.c runtime: change map iteration randomization to use intra-bucket offset 2014-01-14 12:54:05 -08:00
runtime.go runtime, gc: call interface conversion routines by reference. 2013-12-17 16:55:06 -08:00
select.c cmd/gc: fix eval order in select 2012-12-22 16:46:01 -05:00
sinit.c cmd/gc: fix race build 2014-01-16 10:11:06 -05:00
subr.c cmd/gc: add missing dupok flag for interface method wrappers. 2014-01-07 18:25:11 +01:00
swt.c cmd/gc: print expression in 'duplicate case in switch' error 2013-09-20 15:15:43 -04:00
typecheck.c cmd/gc: return canonical Node* from temp 2014-01-14 10:43:13 -05:00
unsafe.c cmd/gc: fix some overflows in the compiler 2013-04-29 22:44:40 -07:00
unsafe.go gc: delete old unsafe functions 2012-02-13 15:37:35 -05:00
walk.c cmd/6g, cmd/gc, cmd/ld: fix Plan 9 amd64 warnings 2013-12-18 20:20:46 +01:00
y.tab.c cmd/gc: qualified embedded fields with owner package. 2013-10-02 12:27:33 -04:00
y.tab.h cmd/gc: qualified embedded fields with owner package. 2013-10-02 12:27:33 -04:00
yerr.h cmd/gc: qualified embedded fields with owner package. 2013-10-02 12:27:33 -04:00