Commit e360f7c4 authored by Keith Randall's avatar Keith Randall

cmd/compile: keep JMPs around with -N

When -N, make sure we don't drop every instruction from
a block, even ones which would otherwise be empty.
Helps keep line numbers around for debugging, particularly
for break and continue statements (which often compile
down to nothing).

Fixes #14379

Change-Id: I33722c4f0dcd502f146fa48af262ba3a477c959a
Reviewed-on: https://go-review.googlesource.com/19854
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarMinux Ma <minux@golang.org>
parent c4cb365e
...@@ -236,6 +236,7 @@ func Main() { ...@@ -236,6 +236,7 @@ func Main() {
} }
Ctxt.Flag_shared = int32(flag_shared) Ctxt.Flag_shared = int32(flag_shared)
Ctxt.Flag_dynlink = flag_dynlink Ctxt.Flag_dynlink = flag_dynlink
Ctxt.Flag_optimize = Debug['N'] == 0
Ctxt.Debugasm = int32(Debug['S']) Ctxt.Debugasm = int32(Debug['S'])
Ctxt.Debugvlog = int32(Debug['v']) Ctxt.Debugvlog = int32(Debug['v'])
......
...@@ -138,15 +138,16 @@ func fixjmp(firstp *obj.Prog) { ...@@ -138,15 +138,16 @@ func fixjmp(firstp *obj.Prog) {
fmt.Printf("%v\n", p) fmt.Printf("%v\n", p)
} }
if p.As != obj.ACALL && p.To.Type == obj.TYPE_BRANCH && p.To.Val.(*obj.Prog) != nil && p.To.Val.(*obj.Prog).As == obj.AJMP { if p.As != obj.ACALL && p.To.Type == obj.TYPE_BRANCH && p.To.Val.(*obj.Prog) != nil && p.To.Val.(*obj.Prog).As == obj.AJMP {
p.To.Val = chasejmp(p.To.Val.(*obj.Prog), &jmploop) if Debug['N'] == 0 {
if Debug['R'] != 0 && Debug['v'] != 0 { p.To.Val = chasejmp(p.To.Val.(*obj.Prog), &jmploop)
fmt.Printf("->%v\n", p) if Debug['R'] != 0 && Debug['v'] != 0 {
fmt.Printf("->%v\n", p)
}
} }
} }
p.Opt = dead p.Opt = dead
} }
if Debug['R'] != 0 && Debug['v'] != 0 { if Debug['R'] != 0 && Debug['v'] != 0 {
fmt.Printf("\n") fmt.Printf("\n")
} }
...@@ -186,7 +187,7 @@ func fixjmp(firstp *obj.Prog) { ...@@ -186,7 +187,7 @@ func fixjmp(firstp *obj.Prog) {
// pass 4: elide JMP to next instruction. // pass 4: elide JMP to next instruction.
// only safe if there are no jumps to JMPs anymore. // only safe if there are no jumps to JMPs anymore.
if jmploop == 0 { if jmploop == 0 && Debug['N'] == 0 {
var last *obj.Prog var last *obj.Prog
for p := firstp; p != nil; p = p.Link { for p := firstp; p != nil; p = p.Link {
if p.As == obj.AJMP && p.To.Type == obj.TYPE_BRANCH && p.To.Val == p.Link { if p.As == obj.AJMP && p.To.Type == obj.TYPE_BRANCH && p.To.Val == p.Link {
......
...@@ -572,6 +572,7 @@ type Link struct { ...@@ -572,6 +572,7 @@ type Link struct {
Debugpcln int32 Debugpcln int32
Flag_shared int32 Flag_shared int32
Flag_dynlink bool Flag_dynlink bool
Flag_optimize bool
Bso *Biobuf Bso *Biobuf
Pathname string Pathname string
Windows int32 Windows int32
......
...@@ -295,7 +295,9 @@ func Flushplist(ctxt *Link) { ...@@ -295,7 +295,9 @@ func Flushplist(ctxt *Link) {
for s := text; s != nil; s = s.Next { for s := text; s != nil; s = s.Next {
mkfwd(s) mkfwd(s)
linkpatch(ctxt, s) linkpatch(ctxt, s)
ctxt.Arch.Follow(ctxt, s) if ctxt.Flag_optimize {
ctxt.Arch.Follow(ctxt, s)
}
ctxt.Arch.Preprocess(ctxt, s) ctxt.Arch.Preprocess(ctxt, s)
ctxt.Arch.Assemble(ctxt, s) ctxt.Arch.Assemble(ctxt, s)
fieldtrack(ctxt, s) fieldtrack(ctxt, s)
......
...@@ -202,13 +202,15 @@ func linkpatch(ctxt *Link, sym *LSym) { ...@@ -202,13 +202,15 @@ func linkpatch(ctxt *Link, sym *LSym) {
p.Pcond = q p.Pcond = q
} }
for p := sym.Text; p != nil; p = p.Link { if ctxt.Flag_optimize {
p.Mark = 0 /* initialization for follow */ for p := sym.Text; p != nil; p = p.Link {
if p.Pcond != nil { p.Mark = 0 /* initialization for follow */
p.Pcond = brloop(ctxt, p.Pcond)
if p.Pcond != nil { if p.Pcond != nil {
if p.To.Type == TYPE_BRANCH { p.Pcond = brloop(ctxt, p.Pcond)
p.To.Offset = p.Pcond.Pc if p.Pcond != nil {
if p.To.Type == TYPE_BRANCH {
p.To.Offset = p.Pcond.Pc
}
} }
} }
} }
......
...@@ -110,6 +110,7 @@ func Linknew(arch *LinkArch) *Link { ...@@ -110,6 +110,7 @@ func Linknew(arch *LinkArch) *Link {
ctxt.Goarm = Getgoarm() ctxt.Goarm = Getgoarm()
} }
ctxt.Flag_optimize = true
return ctxt return ctxt
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment