Skip to content

Commit

Permalink
cmd/compile: keep JMPs around with -N
Browse files Browse the repository at this point in the history
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 <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Minux Ma <[email protected]>
  • Loading branch information
randall77 committed Feb 24, 2016
1 parent c4cb365 commit e360f7c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/cmd/compile/internal/gc/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func Main() {
}
Ctxt.Flag_shared = int32(flag_shared)
Ctxt.Flag_dynlink = flag_dynlink
Ctxt.Flag_optimize = Debug['N'] == 0

Ctxt.Debugasm = int32(Debug['S'])
Ctxt.Debugvlog = int32(Debug['v'])
Expand Down
11 changes: 6 additions & 5 deletions src/cmd/compile/internal/gc/popt.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,16 @@ func fixjmp(firstp *obj.Prog) {
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 {
p.To.Val = chasejmp(p.To.Val.(*obj.Prog), &jmploop)
if Debug['R'] != 0 && Debug['v'] != 0 {
fmt.Printf("->%v\n", p)
if Debug['N'] == 0 {
p.To.Val = chasejmp(p.To.Val.(*obj.Prog), &jmploop)
if Debug['R'] != 0 && Debug['v'] != 0 {
fmt.Printf("->%v\n", p)
}
}
}

p.Opt = dead
}

if Debug['R'] != 0 && Debug['v'] != 0 {
fmt.Printf("\n")
}
Expand Down Expand Up @@ -186,7 +187,7 @@ func fixjmp(firstp *obj.Prog) {

// pass 4: elide JMP to next instruction.
// only safe if there are no jumps to JMPs anymore.
if jmploop == 0 {
if jmploop == 0 && Debug['N'] == 0 {
var last *obj.Prog
for p := firstp; p != nil; p = p.Link {
if p.As == obj.AJMP && p.To.Type == obj.TYPE_BRANCH && p.To.Val == p.Link {
Expand Down
1 change: 1 addition & 0 deletions src/cmd/internal/obj/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ type Link struct {
Debugpcln int32
Flag_shared int32
Flag_dynlink bool
Flag_optimize bool
Bso *Biobuf
Pathname string
Windows int32
Expand Down
4 changes: 3 additions & 1 deletion src/cmd/internal/obj/objfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ func Flushplist(ctxt *Link) {
for s := text; s != nil; s = s.Next {
mkfwd(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.Assemble(ctxt, s)
fieldtrack(ctxt, s)
Expand Down
14 changes: 8 additions & 6 deletions src/cmd/internal/obj/pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ func linkpatch(ctxt *Link, sym *LSym) {
p.Pcond = q
}

for p := sym.Text; p != nil; p = p.Link {
p.Mark = 0 /* initialization for follow */
if p.Pcond != nil {
p.Pcond = brloop(ctxt, p.Pcond)
if ctxt.Flag_optimize {
for p := sym.Text; p != nil; p = p.Link {
p.Mark = 0 /* initialization for follow */
if p.Pcond != nil {
if p.To.Type == TYPE_BRANCH {
p.To.Offset = p.Pcond.Pc
p.Pcond = brloop(ctxt, p.Pcond)
if p.Pcond != nil {
if p.To.Type == TYPE_BRANCH {
p.To.Offset = p.Pcond.Pc
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/cmd/internal/obj/sym.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func Linknew(arch *LinkArch) *Link {
ctxt.Goarm = Getgoarm()
}

ctxt.Flag_optimize = true
return ctxt
}

Expand Down

0 comments on commit e360f7c

Please sign in to comment.