aboutsummaryrefslogtreecommitdiff
path: root/gcc/go/gofrontend/statements.cc
AgeCommit message (Collapse)AuthorFilesLines
2022-10-10compiler: only build thunk struct type when it is neededIan Lance Taylor1-25/+9
Instead of building the thunk struct type in the determine_types pass, build it when we need it. That ensures that we are consistent in determining whether an argument is constant. We no longer need to add a field for a call to recover, as the simplify_thunk_statements pass runs after the build_recover_thunks pass, so the additional argument will already have been added to the call. The test case is https://go.dev/cl/440297. Fixes golang/go#56109 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/440298
2022-06-24compiler: use bool for comma-ok if not already booleanIan Lance Taylor1-7/+11
If a comma-ok variable already has a type, and that type is not a boolean type, then set the type of the temporary variable to bool. Otherwise we may try to convert an unnamed bool type to an interface type, which will fail. But we don't want to always use bool, because the type of the comma-ok variable may be a named bool type, in which case the assignment would fail (or need an explicit conversion). The test case is https://go.dev/cl/404496. Fixes golang/go#52535 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/413894
2022-05-17compiler: load LHS subexpressions of op= assignment only onceIan Lance Taylor1-0/+10
Fixes golang/go#52811 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/405617
2021-08-03compile, runtime: make selectnbrecv return two valuesIan Lance Taylor1-28/+31
The only different between selectnbrecv and selectnbrecv2 is the later set the input pointer value by second return value from chanrecv. So by making selectnbrecv return two values from chanrecv, we can get rid of selectnbrecv2, the compiler can now call only selectnbrecv and generate simpler code. This is the gofrontend version of https://golang.org/cl/292890. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/339529
2021-05-24compiler: mark global variables whose address is takenIan Lance Taylor1-2/+4
To implement this, change the backend to use flag bits for variables. Fixes https://gcc.gnu.org/PR100537 PR go/100537 * go-gcc.cc (class Gcc_backend): Update methods that create variables to take a flags parameter. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/322129
2021-03-17compiler: copy receiver argument for go/defer of method callIan Lance Taylor1-0/+27
Test case is https://golang.org/cl/302371. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/302270
2021-03-11compiler: create temporaries for heap variablesIan Lance Taylor1-3/+3
The compiler generally doesn't create a temporary for an expression that is a variable, because it's normally valid to simply reload the value from the variable. However, if the variable is in the heap, then loading the value is a pointer indirection. The process of creating GCC IR can cause the variable load and the pointer indirection to be split, such that the second evaluation only does the pointer indirection. If there are conditionals in between the two uses, this can cause the second use to load the pointer from an uninitialized register. Avoid this by introducing a new Expression method that returns whether it is safe to evaluate an expression multiple times, and use it everywhere. The test case is https://golang.org/cl/300789. Fixes golang/go#44383 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/300809
2020-12-22runtime: eliminate scase.kind fieldIan Lance Taylor1-39/+92
This is the gofrontend version of https://golang.org/cl/245125. Original CL description: Currently, we include a "kind" field on scase to distinguish the three kinds of cases in a select statement: sends, receives, and defaults. This commit removes by kind field by instead arranging for the compiler to always place sends before receives, and to provide their counts separately. It also passes an explicit "block bool" parameter to avoid needing to include a default case in the array. It's safe to shuffle cases like this because the runtime will randomize the order they're polled in anyway. For golang/go#40410. This is being brought over to gofrontend as a step toward upgrading to Go1.16beta1. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/279735
2020-12-15compiler: correct grammar in error messageIan Lance Taylor1-1/+1
For golang/go#43200 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/278452
2020-12-07compiler: type of string index expression is byteIan Lance Taylor1-2/+2
To make this work from the do_type method, add "byte" and "rune" to the list of known integer types, and look them up that way rather than via gogo->lookup_global. For golang/go#8745 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/275653
2020-11-30compiler: use correct assignment order for type assertionsIan Lance Taylor1-2/+26
For "a, b := v.(T)" we must set a before b. For golang/go#13433 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/273906
2020-09-22compiler: call runtime.eqtype for non-interface type switch on aixClément Chigot1-9/+18
All type switch clauses must call runtime.eqtype if the linker isn't able to merge type descriptors pointers. Previously, only interface-type clauses were doing it. Updates golang/go#39276 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/255202
2020-01-07compiler, runtime: stop using __go_runtime_errorIan Lance Taylor1-2/+3
Use specific panic functions instead, which are mostly already in the runtime package. Also correct "defer nil" to panic when we execute the defer, rather than throw when we queue it. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/213642 From-SVN: r279979
2019-10-04compiler: adjust code to avoid shadowing local variablesIan Lance Taylor1-8/+9
Also add a couple of missing calls to free after mpz_get_str. This should make the code clean with respect to -Wshadow=local. Based on patch by Bernd Edlinger. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/198837 From-SVN: r276579
2019-09-10compiler: permit inlining constant expressions and expression statementsIan Lance Taylor1-0/+12
This relatively minor change increases the number of inlinable functions/methods in the standard library from 983 to 2179. In particular it permits inlining math/bits/RotateLeftNN. This restores the speed of crypto/sha256 back to what it was before the update to 1.13beta1. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/194340 From-SVN: r275558
2019-08-17compiler, runtime: allocate defer records on the stackIan Lance Taylor1-3/+39
When a defer is executed at most once in a function body, we can allocate the defer record for it on the stack instead of on the heap. This should make defers like this (which are very common) faster. This is a port of CL 171758 from the gc repo. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/190410 From-SVN: r274613
2019-07-23compiler: use correct value type in 2-case select sendIan Lance Taylor1-2/+3
In the channel-send case, the value to be sent may needs an (implicit) type conversion to the channel element type. This CL ensures that we use the correct value type for the send. Fixes golang/go#33235. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/187177 From-SVN: r273743
2019-07-18compiler: fix bug in importing blocks from inline functionsIan Lance Taylor1-0/+2
This patch fixes a buglet in the function body importer. Add hooks for keeping a stack of blocks corresponding to the block nesting in the imported function. This ensures that local variables and temps wind up correctly scoped and don't introduce collisions. New test case for this problem in CL 186717. Fixes golang/go#33158. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/186757 From-SVN: r273577
2019-07-10compiler: add break label in 1,2-case select statement loweringIan Lance Taylor1-0/+8
CL 184998 added optimizations for one- and two-case select statements. But it didn't handle break statement in the select case correctly. Specifically, it didn't add the label definition, so it could result in a dangling goto. This CL fixes this, by adding the label definition. A test case is CL 185520. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/185519 From-SVN: r273359
2019-07-04compiler: optimize 0,1,2-case select statementIan Lance Taylor1-0/+229
For a select statement with zero-, one-, or two-case with a default case, we can generate simpler code instead of calling the generic selectgo. A zero-case select is just blocking the execution. A one-case select is mostly just executing the case. A two-case select with a default case is a non-blocking send or receive. We add these special cases for lowering a select statement. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/184998 From-SVN: r273034
2019-07-04compiler: fix indentation of select statement AST dumpIan Lance Taylor1-0/+1
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/184997 From-SVN: r273032
2019-07-02compiler: use builtin memset for non-pointer memclrCherry Zhang1-6/+12
For zeroing a range of memory that doesn't contain pointer, we can use builtin memset directly. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/184438 * go-gcc.cc (Gcc_backend::Gcc_backend): Define __builtin_memset. From-SVN: r272944
2019-06-23compiler: edit error messages to avoid -Wformat-diag warningsIan Lance Taylor1-1/+1
GCC recently introduced -Wformat-diag to scrutinize GCC error messages. It reports a number of warnings about gofrontend code, such as: ../../trunk/gcc/go/gofrontend/import.cc: In member function ‘Type* Import::type_for_index(int, const string&, size_t, bool*)’: ../../trunk/gcc/go/gofrontend/import.cc:1129:48: warning: unquoted operator ‘>=’ in format [-Wformat-diag] 1129 | "error in %s at %lu: bad type index %d >= %d", | ^~ ../../trunk/gcc/go/gofrontend/ast-dump.cc: In member function ‘void Ast_dump_context::dump(Gogo*, const char*)’: ../../trunk/gcc/go/gofrontend/ast-dump.cc:203:25: warning: unquoted option name ‘-fgo-dump-ast’ in format [-Wformat-diag] 203 | "cannot open %s:%m, -fgo-dump-ast ignored", dumpname.c_str()); | ^~~~~~~~~~~~~ ../../trunk/gcc/go/gofrontend/expressions.cc: In static member function ‘static Bexpression* Func_expression::get_code_pointer(Gogo*, Named_object*, Location)’: ../../trunk/gcc/go/gofrontend/expressions.cc:1350:29: warning: misspelled term ‘builtin function’ in format; use ‘built-in function’ instead [-Wformat-diag] 1350 | "invalid use of special builtin function %qs; must be called", | ^~~~~~~~~~~~~~~~ ../../trunk/gcc/go/gofrontend/gogo.cc: In member function ‘void Gogo::add_linkname(const string&, bool, const string&, Location)’: ../../trunk/gcc/go/gofrontend/gogo.cc:2527:4: warning: unquoted sequence of 2 consecutive punctuation characters ‘//’ in format [-Wformat-diag] 2527 | ("%s is not a function; " | ~^~~~~~~~~~~~~~~~~~~~~~~~ 2528 | "//go:linkname is only supported for functions"), | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This CL edits error messages to avoid these warnings. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/183497 * go.test/test/blank1.go: Update for diagnostic message changes. From-SVN: r272608
2019-06-23compiler: add go_debug and use it for debug messagesIan Lance Taylor1-2/+2
GCC recently added a new warning -Wformat-diag which does a lot of rigorous checks on GCC diagnostic messages. This produces a number of unnecessary diagnostics on gofrontend diagnostic output, such as ../../trunk/gcc/go/gofrontend/escape.cc: In member function ‘virtual int Escape_analysis_assign::statement(Block*, size_t*, Statement*)’: ../../trunk/gcc/go/gofrontend/escape.cc:1336:33: warning: spurious leading punctuation sequence ‘[’ in format [-Wformat-diag] 1336 | go_inform(s->location(), "[%d] %s esc: %s", | ^ ../../trunk/gcc/go/gofrontend/escape.cc: In member function ‘void Escape_analysis_assign::call(Call_expression*)’: ../../trunk/gcc/go/gofrontend/escape.cc:1964:17: warning: unquoted operator ‘::’ in format [-Wformat-diag] 1964 | "esccall:: indirect call <- %s, untracked", | ^~ ../../trunk/gcc/go/gofrontend/escape.cc:1964:34: warning: unbalanced punctuation character ‘<’ in format [-Wformat-diag] 1964 | "esccall:: indirect call <- %s, untracked", | ^ Avoid these messages by adding a new function go_debug that uses only printf formatting, not GCC diagnostic formatting, and change all the optimization debugging messages to use it. None of the debugging messages used the GCC diagnostic formatting specifiers anyhow. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/183437 From-SVN: r272607
2019-06-21compiler: open code some type assertionsIan Lance Taylor1-12/+13
Now that type equality is just simple pointer equality, we can open code some type assertions instead of making runtime calls. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/182977 From-SVN: r272577
2019-06-21compiler: open code string slice expressionsIan Lance Taylor1-0/+12
Currently a string slice expression is implemented with a runtime call __go_string_slice. Change it to open code it, which is more efficient, and allows the backend to further optimize it. Also omit the write barrier for length-only update (i.e. s = s[:n]). Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/182540 From-SVN: r272549
2019-06-10compiler: support inlining functions that use index expressionsIan Lance Taylor1-9/+0
Also move the determine_types pass on an inlined function body to one place, rather than doing it ad hoc as needed. This adds 79 new inlinable functions in the standard library, such as bytes.HasPrefix and bytes.LastIndexByte. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/181261 From-SVN: r272133
2019-06-10compiler: permit inlining functions with labels and goto statementsIan Lance Taylor1-11/+193
This permits inlining functions with for loops and some switches, as they are lowered to if and goto statements before exporting them. This by itself only adds three new inlinable functions in the standard library: sort.Search, context.(*emptyCtx).String, and cmd/go/internal/work.(*Builder).disableBuildID. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/181197 From-SVN: r272131
2019-06-07compiler: support inlining functions with if statementsIan Lance Taylor1-18/+105
This increases the number of inlinable functions from 455 to 500. An example of a newly inlinable function is strings.Compare. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/181137 From-SVN: r272045
2019-06-06compiler: permit inlining temporary statements and referencesIan Lance Taylor1-0/+88
This increases the number of inlinable functions from 439 to 455. An example is math/bits.Mul32, which uses temporaries to handle the tuple assignment. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/180837 From-SVN: r272022
2019-06-06compiler: make use of specialized fast map routinesIan Lance Taylor1-40/+105
In the runtime there are specialized fast map routines for certain kep types. This CL lets the compiler make use of these functions, instead of always using the generic ones. As we now generate multiple versions of map delete calls, to make things easier we delay the expansion of the built-in delete function to flatten phase. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/180858 From-SVN: r271983
2019-05-31compiler: optimize append of makeIan Lance Taylor1-0/+3
The gc compiler recognizes append(s, make([]T, n)...), and generates code to directly zero the tail instead of allocating a new slice and copying. This CL lets the Go frontend do basically the same. The difficulty is that at the point we handle append, there may already be temporaries introduced (e.g. in order_evaluations), which makes it hard to find the append-of-make pattern. The compiler could "see through" the value of a temporary, but it is only safe to do if the temporary is not assigned multiple times. For this, we add tracking of assignments and uses for temporaries. This also helps in optimizing non-escape slice make. We already optimize non-escape slice make with constant len/cap to stack allocation. But it failed to handle things like f(make([]T, n)) (where the slice doesn't escape and n is constant), because of the temporary. With tracking of temporary assignments and uses, it can handle this now as well. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/179597 From-SVN: r271822
2019-05-30compiler: intrinsify sync/atomic functionsIan Lance Taylor1-2/+2
Let the Go frontend recognize sync/atomic functions and turn them into intrinsics. Also make sure not to intrinsify calls in go or defer statements. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/178937 From-SVN: r271784
2019-05-16compiler: improve escape analysis on interface conversionsCherry Zhang1-0/+56
If an interface does not escape, it doesn't need a heap allocation to hold the data (for non-direct interface type). This CL improves the escape analysis to track interface conversions, and reduces these allocations. Implicit interface conversions were mostly added late in the compilation pipeline, after the escape analysis. For the escape analysis to see them, we move the introduction of these conversions earlier, right before the escape analysis. Now that the compiler can generate interface conversions inlined, gcc/testsuite/go.test/test/nilptr2.go needs to be adjusted as in golang.org/cl/176579, so the use function does an actual use. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/176459 * go.test/test/nilptr2.go: Change use function to actually do something. From-SVN: r271276
2019-05-10compiler: permit inlining variable declaration statementsIan Lance Taylor1-0/+53
This adds all of two inlinable functions to the standard library: crypto/subtle.ConstantTimeLessOrEq, regexp.(*Regexp).Copy. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/176378 From-SVN: r271063
2019-05-09compiler: avoid copy for string([]byte) conversion used in map keysCherry Zhang1-0/+7
If a string([]byte) conversion is used immediately as a key for a map read, we don't need to copy the backing store of the byte slice, as mapaccess does not keep a reference to it. The gc compiler does more than this: it also avoids the copy if the map key is a composite literal that contains the conversion as a field, like, T{ ... { ..., string(b), ... }, ... }. For now, we just optimize the simple case, which is probably most common. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/176197 * go.dg/mapstring.go: New test. From-SVN: r271044
2019-05-08compiler: add an option to emit optimization diagnosticsCherry Zhang1-0/+4
Add a -fgo-debug-optimization option to emit optimization diagnostics. This can be used for testing optimizations. Apply this to the range clear optimizations of maps and arrays. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/170002 gcc/go: * lang.opt (-fgo-debug-optimization): New option. * go-c.h (struct go_create_gogo_args): Add debug_optimization field. * go-lang.c (go_langhook_init): Set debug_optimization field. * gccgo.texi (Invoking gccgo): Document -fgo-debug-optimization. gcc/testsuite: * go.dg/arrayclear.go: New test. * go.dg/mapclear.go: New test. From-SVN: r270993
2019-05-03compiler: recognize and optimize array range clearIan Lance Taylor1-0/+118
Recognize for i := range a { a[i] = zero } for array or slice a, and rewrite it to call memclr, as the gc compiler does. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/169398 From-SVN: r270862
2019-05-01compiler: recognize and optimize map range clearIan Lance Taylor1-0/+96
Recognize for k := range m { delete(m, k) } for map m, and rewrite it to runtime.mapclear, as the gc compiler does. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/169397 From-SVN: r270780
2018-11-28compiler: inline functions with assignments and return statementsIan Lance Taylor1-2/+53
Support inlining functions that contain only assignments and return statements, with expressions of either constants or parameters. Functions that contain other kinds of statements or expressions are not yet inlined. With this change, about 100 functions in the standard library are inlinable. Reviewed-on: https://go-review.googlesource.com/c/150073 From-SVN: r266573
2018-11-27compiler: import inlinable functions from package dataIan Lance Taylor1-0/+8
Start reading the export data generated by the last change in this series. At this point we will inline direct calls to empty functions and methods defined in different packages. Reviewed-on: https://go-review.googlesource.com/c/150062 From-SVN: r266517
2018-11-26compiler: initial support for exporting function bodiesIan Lance Taylor1-0/+22
Create a framework for putting function bodies in export data. At present only empty functions will be put there, and they will be ignored on import. Later patches will get this to the point of supporting inlining of (some) functions defined in other packages. Reviewed-on: https://go-review.googlesource.com/c/150061 From-SVN: r266490
2018-10-18compiler: rewrite Type::are_identical to use flagsIan Lance Taylor1-3/+8
A single flags parameter replaces the Cmp_tags and errors_are_identical parameters. The existing behavior is unchanged. This is a simplification step for future work that will add a new flag. Reviewed-on: https://go-review.googlesource.com/c/143019 From-SVN: r265293
2018-09-13compiler, runtime: open code selectIan Lance Taylor1-85/+147
This is the gofrontend version of https://golang.org/cl/37933, https://golang.org/cl/37934, and https://golang.org/cl/37935. Open code the initialization of select cases. This is a step toward updating libgo to the 1.11 release. Reviewed-on: https://go-review.googlesource.com/135000 From-SVN: r264290
2018-09-13compiler: omit a couple of write barriersIan Lance Taylor1-2/+17
Omit a write barrier for s = s[0:] for a slice s. In this case the pointer is not changing and no write barrier is required. Omit a write barrier for s = append(s, v) in the case where len(s) < cap(s) (and similarly when appending more values). When the slice has enough capacity the pointer is not changing and no write barrier is required. These changes are required to avoid write barriers in the method randomOrder.reset in the runtime package. That method is called from procresize, at a point where we do not want to allocate memory. Otherwise that method can use a write barrier, allocate memory, and break TestReadMemStats. Reviewed-on: https://go-review.googlesource.com/134219 From-SVN: r264259
2018-02-05compiler: in range, evaluate array if it has receives or callsIan Lance Taylor1-5/+6
The last change was incomplete, in that it did not evaluate the array argument in some cases where it had to be evaluated. This reuses the existing code for checking whether len/cap is constant. Also clean up the use of _ as the second variable in a for/range, which was previously inconsistent depending on whether the statement used = or :=. Updates golang/go#22313 Reviewed-on: https://go-review.googlesource.com/91715 From-SVN: r257377
2018-02-02compiler: don't incorrectly evaluate range variableIan Lance Taylor1-12/+36
The language spec says that in `for i = range x`, in which there is no second iteration variable, if len(x) is constant, then x is not evaluated. This only matters when x is an expression that panics but whose type is an array type; in such a case, we should not evaluate x, since len of any array type is a constant. Fixes golang/go#22313 Reviewed-on: https://go-review.googlesource.com/91555 From-SVN: r257330
2018-01-24compiler: rationalize external symbol namesIan Lance Taylor1-1/+1
Encode all external symbol names using only ASCII alphanumeric characters, underscore, and dot. Use a scheme that can be reliably demangled to a somewhat readable version as described in the long comment in names.cc. A minor cleanup discovered during this was that we were treating function types as different if one had a NULL parameters_ field and another has a non-NULL parameters_ field that has no parameters. This worked because we mangled them slightly differently. We now mangle them the same, so we treat them as equal, as we should anyhow. Reviewed-on: https://go-review.googlesource.com/89555 * go.go-torture/execute/names-1.go: New test. From-SVN: r257033
2018-01-09compiler: stack allocate defer thunkIan Lance Taylor1-0/+2
Defer statement may need to allocate a thunk. When it is not inside a loop, this can be stack allocated, as it runs before the function finishes. Reviewed-on: https://go-review.googlesource.com/85639 From-SVN: r256410
2017-12-02compiler: avoid GCC middle-end control warningsIan Lance Taylor1-3/+8
GCC has started emitting "control reaches end of non-void function" warnings. Avoid them for Go by 1) marking the builtin function panic and the compiler-generated function __go_runtime_error as not returning and 2) adding a default case to the switch used for select statements that simply calls __builtin_unreachable. Fixes golang/go#22767 Reviewed-on: https://go-review.googlesource.com/80416 * go-gcc.cc (Gcc_backend::Gcc_backend): Define __builtin_unreachable. (Gcc_backend::function): Add does_not_return parameter. From-SVN: r255346