File tree Expand file tree Collapse file tree 2 files changed +65
-1
lines changed
src/cmd/compile/internal/gc Expand file tree Collapse file tree 2 files changed +65
-1
lines changed Original file line number Diff line number Diff line change @@ -3926,24 +3926,45 @@ func deadcode(fn *Node) {
3926
3926
}
3927
3927
3928
3928
func deadcodeslice (nn Nodes ) {
3929
- for _ , n := range nn .Slice () {
3929
+ for i , n := range nn .Slice () {
3930
+ // Cut is set to true when all nodes after i'th position
3931
+ // should be removed.
3932
+ // In other words, it marks whole slice "tail" as dead.
3933
+ cut := false
3930
3934
if n == nil {
3931
3935
continue
3932
3936
}
3933
3937
if n .Op == OIF {
3934
3938
n .Left = deadcodeexpr (n .Left )
3935
3939
if Isconst (n .Left , CTBOOL ) {
3940
+ var body Nodes
3936
3941
if n .Left .Bool () {
3937
3942
n .Rlist = Nodes {}
3943
+ body = n .Nbody
3938
3944
} else {
3939
3945
n .Nbody = Nodes {}
3946
+ body = n .Rlist
3947
+ }
3948
+ // If "then" or "else" branch ends with panic or return statement,
3949
+ // it is safe to remove all statements after this node.
3950
+ // isterminating is not used to avoid goto-related complications.
3951
+ if body := body .Slice (); len (body ) != 0 {
3952
+ switch body [(len (body ) - 1 )].Op {
3953
+ case ORETURN , ORETJMP , OPANIC :
3954
+ cut = true
3955
+ }
3940
3956
}
3941
3957
}
3942
3958
}
3959
+
3943
3960
deadcodeslice (n .Ninit )
3944
3961
deadcodeslice (n .Nbody )
3945
3962
deadcodeslice (n .List )
3946
3963
deadcodeslice (n .Rlist )
3964
+ if cut {
3965
+ * nn .slice = nn .Slice ()[:i + 1 ]
3966
+ break
3967
+ }
3947
3968
}
3948
3969
}
3949
3970
Original file line number Diff line number Diff line change
1
+ // errorcheck -0 -m
2
+
3
+ // Copyright 2018 The Go Authors. All rights reserved.
4
+ // Use of this source code is governed by a BSD-style
5
+ // license that can be found in the LICENSE file.
6
+
7
+ // Issue 23521: improve early DCE for if without explicit else.
8
+
9
+ package p
10
+
11
+ //go:noinline
12
+ func nonleaf () {}
13
+
14
+ const truth = true
15
+
16
+ func f () int { // ERROR "can inline f"
17
+ if truth {
18
+ return 0
19
+ }
20
+ // If everything below is removed, as it should,
21
+ // function f should be inlineable.
22
+ nonleaf ()
23
+ for {
24
+ panic ("" )
25
+ }
26
+ }
27
+
28
+ func g () int { // ERROR "can inline g"
29
+ return f () // ERROR "inlining call to f"
30
+ }
31
+
32
+ func f2 () int { // ERROR "can inline f2"
33
+ if ! truth {
34
+ nonleaf ()
35
+ } else {
36
+ return 0
37
+ }
38
+ panic ("" )
39
+ }
40
+
41
+ func g2 () int { // ERROR "can inline g2"
42
+ return f2 () // ERROR "inlining call to f2"
43
+ }
You can’t perform that action at this time.
0 commit comments