github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/cmd/compile/internal/gc/popt.go (about) 1 // Derived from Inferno utils/6c/gc.h 2 // http://code.google.com/p/inferno-os/source/browse/utils/6c/gc.h 3 // 4 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. 5 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net) 6 // Portions Copyright © 1997-1999 Vita Nuova Limited 7 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com) 8 // Portions Copyright © 2004,2006 Bruce Ellis 9 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net) 10 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others 11 // Portions Copyright © 2009 The Go Authors. All rights reserved. 12 // 13 // Permission is hereby granted, free of charge, to any person obtaining a copy 14 // of this software and associated documentation files (the "Software"), to deal 15 // in the Software without restriction, including without limitation the rights 16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 // copies of the Software, and to permit persons to whom the Software is 18 // furnished to do so, subject to the following conditions: 19 // 20 // The above copyright notice and this permission notice shall be included in 21 // all copies or substantial portions of the Software. 22 // 23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 // THE SOFTWARE. 30 31 // "Portable" optimizations. 32 33 package gc 34 35 import ( 36 "cmd/internal/obj" 37 "fmt" 38 "sort" 39 "strings" 40 ) 41 42 type OptStats struct { 43 Ncvtreg int32 44 Nspill int32 45 Nreload int32 46 Ndelmov int32 47 Nvar int32 48 Naddr int32 49 } 50 51 var Ostats OptStats 52 53 var noreturn_symlist [10]*Sym 54 55 // p is a call instruction. Does the call fail to return? 56 func Noreturn(p *obj.Prog) bool { 57 if noreturn_symlist[0] == nil { 58 noreturn_symlist[0] = Pkglookup("panicindex", Runtimepkg) 59 noreturn_symlist[1] = Pkglookup("panicslice", Runtimepkg) 60 noreturn_symlist[2] = Pkglookup("throwinit", Runtimepkg) 61 noreturn_symlist[3] = Pkglookup("gopanic", Runtimepkg) 62 noreturn_symlist[4] = Pkglookup("panicwrap", Runtimepkg) 63 noreturn_symlist[5] = Pkglookup("throwreturn", Runtimepkg) 64 noreturn_symlist[6] = Pkglookup("selectgo", Runtimepkg) 65 noreturn_symlist[7] = Pkglookup("block", Runtimepkg) 66 } 67 68 if p.To.Node == nil { 69 return false 70 } 71 s := ((p.To.Node).(*Node)).Sym 72 if s == nil { 73 return false 74 } 75 for i := 0; noreturn_symlist[i] != nil; i++ { 76 if s == noreturn_symlist[i] { 77 return true 78 } 79 } 80 return false 81 } 82 83 // JMP chasing and removal. 84 // 85 // The code generator depends on being able to write out jump 86 // instructions that it can jump to now but fill in later. 87 // the linker will resolve them nicely, but they make the code 88 // longer and more difficult to follow during debugging. 89 // Remove them. 90 91 /* what instruction does a JMP to p eventually land on? */ 92 func chasejmp(p *obj.Prog, jmploop *int) *obj.Prog { 93 n := 0 94 for p != nil && p.As == obj.AJMP && p.To.Type == obj.TYPE_BRANCH { 95 n++ 96 if n > 10 { 97 *jmploop = 1 98 break 99 } 100 101 p = p.To.Val.(*obj.Prog) 102 } 103 104 return p 105 } 106 107 /* 108 * reuse reg pointer for mark/sweep state. 109 * leave reg==nil at end because alive==nil. 110 */ 111 var alive interface{} = nil 112 var dead interface{} = 1 113 114 /* mark all code reachable from firstp as alive */ 115 func mark(firstp *obj.Prog) { 116 for p := firstp; p != nil; p = p.Link { 117 if p.Opt != dead { 118 break 119 } 120 p.Opt = alive 121 if p.As != obj.ACALL && p.To.Type == obj.TYPE_BRANCH && p.To.Val.(*obj.Prog) != nil { 122 mark(p.To.Val.(*obj.Prog)) 123 } 124 if p.As == obj.AJMP || p.As == obj.ARET || p.As == obj.AUNDEF { 125 break 126 } 127 } 128 } 129 130 func fixjmp(firstp *obj.Prog) { 131 if Debug['R'] != 0 && Debug['v'] != 0 { 132 fmt.Printf("\nfixjmp\n") 133 } 134 135 // pass 1: resolve jump to jump, mark all code as dead. 136 jmploop := 0 137 138 for p := firstp; p != nil; p = p.Link { 139 if Debug['R'] != 0 && Debug['v'] != 0 { 140 fmt.Printf("%v\n", p) 141 } 142 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 { 143 p.To.Val = chasejmp(p.To.Val.(*obj.Prog), &jmploop) 144 if Debug['R'] != 0 && Debug['v'] != 0 { 145 fmt.Printf("->%v\n", p) 146 } 147 } 148 149 p.Opt = dead 150 } 151 152 if Debug['R'] != 0 && Debug['v'] != 0 { 153 fmt.Printf("\n") 154 } 155 156 // pass 2: mark all reachable code alive 157 mark(firstp) 158 159 // pass 3: delete dead code (mostly JMPs). 160 var last *obj.Prog 161 162 for p := firstp; p != nil; p = p.Link { 163 if p.Opt == dead { 164 if p.Link == nil && p.As == obj.ARET && last != nil && last.As != obj.ARET { 165 // This is the final ARET, and the code so far doesn't have one. 166 // Let it stay. The register allocator assumes that all live code in 167 // the function can be traversed by starting at all the RET instructions 168 // and following predecessor links. If we remove the final RET, 169 // this assumption will not hold in the case of an infinite loop 170 // at the end of a function. 171 // Keep the RET but mark it dead for the liveness analysis. 172 p.Mode = 1 173 } else { 174 if Debug['R'] != 0 && Debug['v'] != 0 { 175 fmt.Printf("del %v\n", p) 176 } 177 continue 178 } 179 } 180 181 if last != nil { 182 last.Link = p 183 } 184 last = p 185 } 186 187 last.Link = nil 188 189 // pass 4: elide JMP to next instruction. 190 // only safe if there are no jumps to JMPs anymore. 191 if jmploop == 0 { 192 var last *obj.Prog 193 for p := firstp; p != nil; p = p.Link { 194 if p.As == obj.AJMP && p.To.Type == obj.TYPE_BRANCH && p.To.Val == p.Link { 195 if Debug['R'] != 0 && Debug['v'] != 0 { 196 fmt.Printf("del %v\n", p) 197 } 198 continue 199 } 200 201 if last != nil { 202 last.Link = p 203 } 204 last = p 205 } 206 207 last.Link = nil 208 } 209 210 if Debug['R'] != 0 && Debug['v'] != 0 { 211 fmt.Printf("\n") 212 for p := firstp; p != nil; p = p.Link { 213 fmt.Printf("%v\n", p) 214 } 215 fmt.Printf("\n") 216 } 217 } 218 219 // Control flow analysis. The Flow structures hold predecessor and successor 220 // information as well as basic loop analysis. 221 // 222 // graph = flowstart(firstp, 0); 223 // ... use flow graph ... 224 // flowend(graph); // free graph 225 // 226 // Typical uses of the flow graph are to iterate over all the flow-relevant instructions: 227 // 228 // for(f = graph->start; f != nil; f = f->link) 229 // 230 // or, given an instruction f, to iterate over all the predecessors, which is 231 // f->p1 and this list: 232 // 233 // for(f2 = f->p2; f2 != nil; f2 = f2->p2link) 234 // 235 // The size argument to flowstart specifies an amount of zeroed memory 236 // to allocate in every f->data field, for use by the client. 237 // If size == 0, f->data will be nil. 238 239 var flowmark int 240 241 // MaxFlowProg is the maximum size program (counted in instructions) 242 // for which the flow code will build a graph. Functions larger than this limit 243 // will not have flow graphs and consequently will not be optimized. 244 const MaxFlowProg = 50000 245 246 func Flowstart(firstp *obj.Prog, newData func() interface{}) *Graph { 247 // Count and mark instructions to annotate. 248 nf := 0 249 250 for p := firstp; p != nil; p = p.Link { 251 p.Opt = nil // should be already, but just in case 252 Thearch.Proginfo(p) 253 if p.Info.Flags&Skip != 0 { 254 continue 255 } 256 p.Opt = &flowmark 257 nf++ 258 } 259 260 if nf == 0 { 261 return nil 262 } 263 264 if nf >= MaxFlowProg { 265 if Debug['v'] != 0 { 266 Warn("%v is too big (%d instructions)", Curfn.Func.Nname.Sym, nf) 267 } 268 return nil 269 } 270 271 // Allocate annotations and assign to instructions. 272 graph := new(Graph) 273 ff := make([]Flow, nf) 274 start := &ff[0] 275 id := 0 276 var last *Flow 277 for p := firstp; p != nil; p = p.Link { 278 if p.Opt == nil { 279 continue 280 } 281 f := &ff[0] 282 ff = ff[1:] 283 p.Opt = f 284 f.Prog = p 285 if last != nil { 286 last.Link = f 287 } 288 last = f 289 if newData != nil { 290 f.Data = newData() 291 } 292 f.Id = int32(id) 293 id++ 294 } 295 296 // Fill in pred/succ information. 297 var f1 *Flow 298 var p *obj.Prog 299 for f := start; f != nil; f = f.Link { 300 p = f.Prog 301 if p.Info.Flags&Break == 0 { 302 f1 = f.Link 303 f.S1 = f1 304 f1.P1 = f 305 } 306 307 if p.To.Type == obj.TYPE_BRANCH { 308 if p.To.Val == nil { 309 Fatalf("pnil %v", p) 310 } 311 f1 = p.To.Val.(*obj.Prog).Opt.(*Flow) 312 if f1 == nil { 313 Fatalf("fnil %v / %v", p, p.To.Val.(*obj.Prog)) 314 } 315 if f1 == f { 316 //fatal("self loop %v", p); 317 continue 318 } 319 320 f.S2 = f1 321 f.P2link = f1.P2 322 f1.P2 = f 323 } 324 } 325 326 graph.Start = start 327 graph.Num = nf 328 return graph 329 } 330 331 func Flowend(graph *Graph) { 332 for f := graph.Start; f != nil; f = f.Link { 333 f.Prog.Info.Flags = 0 // drop cached proginfo 334 f.Prog.Opt = nil 335 } 336 } 337 338 /* 339 * find looping structure 340 * 341 * 1) find reverse postordering 342 * 2) find approximate dominators, 343 * the actual dominators if the flow graph is reducible 344 * otherwise, dominators plus some other non-dominators. 345 * See Matthew S. Hecht and Jeffrey D. Ullman, 346 * "Analysis of a Simple Algorithm for Global Data Flow Problems", 347 * Conf. Record of ACM Symp. on Principles of Prog. Langs, Boston, Massachusetts, 348 * Oct. 1-3, 1973, pp. 207-217. 349 * 3) find all nodes with a predecessor dominated by the current node. 350 * such a node is a loop head. 351 * recursively, all preds with a greater rpo number are in the loop 352 */ 353 func postorder(r *Flow, rpo2r []*Flow, n int32) int32 { 354 r.Rpo = 1 355 r1 := r.S1 356 if r1 != nil && r1.Rpo == 0 { 357 n = postorder(r1, rpo2r, n) 358 } 359 r1 = r.S2 360 if r1 != nil && r1.Rpo == 0 { 361 n = postorder(r1, rpo2r, n) 362 } 363 rpo2r[n] = r 364 n++ 365 return n 366 } 367 368 func rpolca(idom []int32, rpo1 int32, rpo2 int32) int32 { 369 if rpo1 == -1 { 370 return rpo2 371 } 372 var t int32 373 for rpo1 != rpo2 { 374 if rpo1 > rpo2 { 375 t = rpo2 376 rpo2 = rpo1 377 rpo1 = t 378 } 379 380 for rpo1 < rpo2 { 381 t = idom[rpo2] 382 if t >= rpo2 { 383 Fatalf("bad idom") 384 } 385 rpo2 = t 386 } 387 } 388 389 return rpo1 390 } 391 392 func doms(idom []int32, r int32, s int32) bool { 393 for s > r { 394 s = idom[s] 395 } 396 return s == r 397 } 398 399 func loophead(idom []int32, r *Flow) bool { 400 src := r.Rpo 401 if r.P1 != nil && doms(idom, src, r.P1.Rpo) { 402 return true 403 } 404 for r = r.P2; r != nil; r = r.P2link { 405 if doms(idom, src, r.Rpo) { 406 return true 407 } 408 } 409 return false 410 } 411 412 func loopmark(rpo2r **Flow, head int32, r *Flow) { 413 if r.Rpo < head || r.Active == head { 414 return 415 } 416 r.Active = head 417 r.Loop += LOOP 418 if r.P1 != nil { 419 loopmark(rpo2r, head, r.P1) 420 } 421 for r = r.P2; r != nil; r = r.P2link { 422 loopmark(rpo2r, head, r) 423 } 424 } 425 426 func flowrpo(g *Graph) { 427 g.Rpo = make([]*Flow, g.Num) 428 idom := make([]int32, g.Num) 429 430 for r1 := g.Start; r1 != nil; r1 = r1.Link { 431 r1.Active = 0 432 } 433 434 rpo2r := g.Rpo 435 d := postorder(g.Start, rpo2r, 0) 436 nr := int32(g.Num) 437 if d > nr { 438 Fatalf("too many reg nodes %d %d", d, nr) 439 } 440 nr = d 441 var r1 *Flow 442 for i := int32(0); i < nr/2; i++ { 443 r1 = rpo2r[i] 444 rpo2r[i] = rpo2r[nr-1-i] 445 rpo2r[nr-1-i] = r1 446 } 447 448 for i := int32(0); i < nr; i++ { 449 rpo2r[i].Rpo = i 450 } 451 452 idom[0] = 0 453 var me int32 454 for i := int32(0); i < nr; i++ { 455 r1 = rpo2r[i] 456 me = r1.Rpo 457 d = -1 458 459 // rpo2r[r->rpo] == r protects against considering dead code, 460 // which has r->rpo == 0. 461 if r1.P1 != nil && rpo2r[r1.P1.Rpo] == r1.P1 && r1.P1.Rpo < me { 462 d = r1.P1.Rpo 463 } 464 for r1 = r1.P2; r1 != nil; r1 = r1.P2link { 465 if rpo2r[r1.Rpo] == r1 && r1.Rpo < me { 466 d = rpolca(idom, d, r1.Rpo) 467 } 468 } 469 idom[i] = d 470 } 471 472 for i := int32(0); i < nr; i++ { 473 r1 = rpo2r[i] 474 r1.Loop++ 475 if r1.P2 != nil && loophead(idom, r1) { 476 loopmark(&rpo2r[0], i, r1) 477 } 478 } 479 480 for r1 := g.Start; r1 != nil; r1 = r1.Link { 481 r1.Active = 0 482 } 483 } 484 485 func Uniqp(r *Flow) *Flow { 486 r1 := r.P1 487 if r1 == nil { 488 r1 = r.P2 489 if r1 == nil || r1.P2link != nil { 490 return nil 491 } 492 } else if r.P2 != nil { 493 return nil 494 } 495 return r1 496 } 497 498 func Uniqs(r *Flow) *Flow { 499 r1 := r.S1 500 if r1 == nil { 501 r1 = r.S2 502 if r1 == nil { 503 return nil 504 } 505 } else if r.S2 != nil { 506 return nil 507 } 508 return r1 509 } 510 511 // The compilers assume they can generate temporary variables 512 // as needed to preserve the right semantics or simplify code 513 // generation and the back end will still generate good code. 514 // This results in a large number of ephemeral temporary variables. 515 // Merge temps with non-overlapping lifetimes and equal types using the 516 // greedy algorithm in Poletto and Sarkar, "Linear Scan Register Allocation", 517 // ACM TOPLAS 1999. 518 519 type TempVar struct { 520 node *Node 521 def *Flow // definition of temp var 522 use *Flow // use list, chained through Flow.data 523 merge *TempVar // merge var with this one 524 start int64 // smallest Prog.pc in live range 525 end int64 // largest Prog.pc in live range 526 addr bool // address taken - no accurate end 527 removed bool // removed from program 528 } 529 530 // startcmp sorts TempVars by start, then id, then symbol name. 531 type startcmp []*TempVar 532 533 func (x startcmp) Len() int { return len(x) } 534 func (x startcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 535 func (x startcmp) Less(i, j int) bool { 536 a := x[i] 537 b := x[j] 538 539 if a.start < b.start { 540 return true 541 } 542 if a.start > b.start { 543 return false 544 } 545 546 // Order what's left by id or symbol name, 547 // just so that sort is forced into a specific ordering, 548 // so that the result of the sort does not depend on 549 // the sort implementation. 550 if a.def != b.def { 551 return int(a.def.Id-b.def.Id) < 0 552 } 553 if a.node != b.node { 554 return a.node.Sym.Name < b.node.Sym.Name 555 } 556 return false 557 } 558 559 // Is n available for merging? 560 func canmerge(n *Node) bool { 561 return n.Class == PAUTO && strings.HasPrefix(n.Sym.Name, "autotmp") 562 } 563 564 func mergetemp(firstp *obj.Prog) { 565 const ( 566 debugmerge = 0 567 ) 568 569 g := Flowstart(firstp, nil) 570 if g == nil { 571 return 572 } 573 574 // Build list of all mergeable variables. 575 var vars []*TempVar 576 for l := Curfn.Func.Dcl; l != nil; l = l.Next { 577 if n := l.N; canmerge(n) { 578 v := &TempVar{} 579 vars = append(vars, v) 580 n.SetOpt(v) 581 v.node = n 582 } 583 } 584 585 // Build list of uses. 586 // We assume that the earliest reference to a temporary is its definition. 587 // This is not true of variables in general but our temporaries are all 588 // single-use (that's why we have so many!). 589 for f := g.Start; f != nil; f = f.Link { 590 p := f.Prog 591 if p.From.Node != nil && ((p.From.Node).(*Node)).Opt() != nil && p.To.Node != nil && ((p.To.Node).(*Node)).Opt() != nil { 592 Fatalf("double node %v", p) 593 } 594 var v *TempVar 595 n, _ := p.From.Node.(*Node) 596 if n != nil { 597 v, _ = n.Opt().(*TempVar) 598 } 599 if v == nil { 600 n, _ = p.To.Node.(*Node) 601 if n != nil { 602 v, _ = n.Opt().(*TempVar) 603 } 604 } 605 if v != nil { 606 if v.def == nil { 607 v.def = f 608 } 609 f.Data = v.use 610 v.use = f 611 if n == p.From.Node && (p.Info.Flags&LeftAddr != 0) { 612 v.addr = true 613 } 614 } 615 } 616 617 if debugmerge > 1 && Debug['v'] != 0 { 618 Dumpit("before", g.Start, 0) 619 } 620 621 nkill := 0 622 623 // Special case. 624 for _, v := range vars { 625 if v.addr { 626 continue 627 } 628 629 // Used in only one instruction, which had better be a write. 630 f := v.use 631 if f != nil && f.Data.(*Flow) == nil { 632 p := f.Prog 633 if p.To.Node == v.node && (p.Info.Flags&RightWrite != 0) && p.Info.Flags&RightRead == 0 { 634 p.As = obj.ANOP 635 p.To = obj.Addr{} 636 v.removed = true 637 if debugmerge > 0 && Debug['v'] != 0 { 638 fmt.Printf("drop write-only %v\n", v.node.Sym) 639 } 640 } else { 641 Fatalf("temp used and not set: %v", p) 642 } 643 nkill++ 644 continue 645 } 646 647 // Written in one instruction, read in the next, otherwise unused, 648 // no jumps to the next instruction. Happens mainly in 386 compiler. 649 f = v.use 650 if f != nil && f.Link == f.Data.(*Flow) && (f.Data.(*Flow)).Data.(*Flow) == nil && Uniqp(f.Link) == f { 651 p := f.Prog 652 p1 := f.Link.Prog 653 const ( 654 SizeAny = SizeB | SizeW | SizeL | SizeQ | SizeF | SizeD 655 ) 656 if p.From.Node == v.node && p1.To.Node == v.node && (p.Info.Flags&Move != 0) && (p.Info.Flags|p1.Info.Flags)&(LeftAddr|RightAddr) == 0 && p.Info.Flags&SizeAny == p1.Info.Flags&SizeAny { 657 p1.From = p.From 658 Thearch.Excise(f) 659 v.removed = true 660 if debugmerge > 0 && Debug['v'] != 0 { 661 fmt.Printf("drop immediate-use %v\n", v.node.Sym) 662 } 663 } 664 665 nkill++ 666 continue 667 } 668 } 669 670 // Traverse live range of each variable to set start, end. 671 // Each flood uses a new value of gen so that we don't have 672 // to clear all the r->active words after each variable. 673 gen := uint32(0) 674 675 for _, v := range vars { 676 gen++ 677 for f := v.use; f != nil; f = f.Data.(*Flow) { 678 mergewalk(v, f, gen) 679 } 680 if v.addr { 681 gen++ 682 for f := v.use; f != nil; f = f.Data.(*Flow) { 683 varkillwalk(v, f, gen) 684 } 685 } 686 } 687 688 // Sort variables by start. 689 bystart := make([]*TempVar, len(vars)) 690 copy(bystart, vars) 691 sort.Sort(startcmp(bystart)) 692 693 // List of in-use variables, sorted by end, so that the ones that 694 // will last the longest are the earliest ones in the array. 695 // The tail inuse[nfree:] holds no-longer-used variables. 696 // In theory we should use a sorted tree so that insertions are 697 // guaranteed O(log n) and then the loop is guaranteed O(n log n). 698 // In practice, it doesn't really matter. 699 inuse := make([]*TempVar, len(bystart)) 700 701 ninuse := 0 702 nfree := len(bystart) 703 for _, v := range bystart { 704 if debugmerge > 0 && Debug['v'] != 0 { 705 fmt.Printf("consider %v: removed=%t\n", Nconv(v.node, obj.FmtSharp), v.removed) 706 } 707 708 if v.removed { 709 continue 710 } 711 712 // Expire no longer in use. 713 for ninuse > 0 && inuse[ninuse-1].end < v.start { 714 ninuse-- 715 nfree-- 716 inuse[nfree] = inuse[ninuse] 717 } 718 719 if debugmerge > 0 && Debug['v'] != 0 { 720 fmt.Printf("consider %v: removed=%t nfree=%d nvar=%d\n", Nconv(v.node, obj.FmtSharp), v.removed, nfree, len(bystart)) 721 } 722 723 // Find old temp to reuse if possible. 724 t := v.node.Type 725 726 for j := nfree; j < len(inuse); j++ { 727 v1 := inuse[j] 728 if debugmerge > 0 && Debug['v'] != 0 { 729 fmt.Printf("consider %v: maybe %v: type=%v,%v addrtaken=%v,%v\n", Nconv(v.node, obj.FmtSharp), Nconv(v1.node, obj.FmtSharp), t, v1.node.Type, v.node.Addrtaken, v1.node.Addrtaken) 730 } 731 732 // Require the types to match but also require the addrtaken bits to match. 733 // If a variable's address is taken, that disables registerization for the individual 734 // words of the variable (for example, the base,len,cap of a slice). 735 // We don't want to merge a non-addressed var with an addressed one and 736 // inhibit registerization of the former. 737 if Eqtype(t, v1.node.Type) && v.node.Addrtaken == v1.node.Addrtaken { 738 inuse[j] = inuse[nfree] 739 nfree++ 740 if v1.merge != nil { 741 v.merge = v1.merge 742 } else { 743 v.merge = v1 744 } 745 nkill++ 746 break 747 } 748 } 749 750 // Sort v into inuse. 751 j := ninuse 752 ninuse++ 753 754 for j > 0 && inuse[j-1].end < v.end { 755 inuse[j] = inuse[j-1] 756 j-- 757 } 758 759 inuse[j] = v 760 } 761 762 if debugmerge > 0 && Debug['v'] != 0 { 763 fmt.Printf("%v [%d - %d]\n", Curfn.Func.Nname.Sym, len(vars), nkill) 764 for _, v := range vars { 765 fmt.Printf("var %v %v %d-%d", Nconv(v.node, obj.FmtSharp), v.node.Type, v.start, v.end) 766 if v.addr { 767 fmt.Printf(" addr=true") 768 } 769 if v.removed { 770 fmt.Printf(" removed=true") 771 } 772 if v.merge != nil { 773 fmt.Printf(" merge %v", Nconv(v.merge.node, obj.FmtSharp)) 774 } 775 if v.start == v.end && v.def != nil { 776 fmt.Printf(" %v", v.def.Prog) 777 } 778 fmt.Printf("\n") 779 } 780 781 if debugmerge > 1 && Debug['v'] != 0 { 782 Dumpit("after", g.Start, 0) 783 } 784 } 785 786 // Update node references to use merged temporaries. 787 for f := g.Start; f != nil; f = f.Link { 788 p := f.Prog 789 n, _ := p.From.Node.(*Node) 790 if n != nil { 791 v, _ := n.Opt().(*TempVar) 792 if v != nil && v.merge != nil { 793 p.From.Node = v.merge.node 794 } 795 } 796 n, _ = p.To.Node.(*Node) 797 if n != nil { 798 v, _ := n.Opt().(*TempVar) 799 if v != nil && v.merge != nil { 800 p.To.Node = v.merge.node 801 } 802 } 803 } 804 805 // Delete merged nodes from declaration list. 806 for lp := &Curfn.Func.Dcl; ; { 807 l := *lp 808 if l == nil { 809 break 810 } 811 812 Curfn.Func.Dcl.End = l 813 n := l.N 814 v, _ := n.Opt().(*TempVar) 815 if v != nil && (v.merge != nil || v.removed) { 816 *lp = l.Next 817 continue 818 } 819 820 lp = &l.Next 821 } 822 823 // Clear aux structures. 824 for _, v := range vars { 825 v.node.SetOpt(nil) 826 } 827 828 Flowend(g) 829 } 830 831 func mergewalk(v *TempVar, f0 *Flow, gen uint32) { 832 var p *obj.Prog 833 var f1 *Flow 834 835 for f1 = f0; f1 != nil; f1 = f1.P1 { 836 if uint32(f1.Active) == gen { 837 break 838 } 839 f1.Active = int32(gen) 840 p = f1.Prog 841 if v.end < p.Pc { 842 v.end = p.Pc 843 } 844 if f1 == v.def { 845 v.start = p.Pc 846 break 847 } 848 } 849 850 var f2 *Flow 851 for f := f0; f != f1; f = f.P1 { 852 for f2 = f.P2; f2 != nil; f2 = f2.P2link { 853 mergewalk(v, f2, gen) 854 } 855 } 856 } 857 858 func varkillwalk(v *TempVar, f0 *Flow, gen uint32) { 859 var p *obj.Prog 860 var f1 *Flow 861 862 for f1 = f0; f1 != nil; f1 = f1.S1 { 863 if uint32(f1.Active) == gen { 864 break 865 } 866 f1.Active = int32(gen) 867 p = f1.Prog 868 if v.end < p.Pc { 869 v.end = p.Pc 870 } 871 if v.start > p.Pc { 872 v.start = p.Pc 873 } 874 if p.As == obj.ARET || (p.As == obj.AVARKILL && p.To.Node == v.node) { 875 break 876 } 877 } 878 879 for f := f0; f != f1; f = f.S1 { 880 varkillwalk(v, f.S2, gen) 881 } 882 } 883 884 // Eliminate redundant nil pointer checks. 885 // 886 // The code generation pass emits a CHECKNIL for every possibly nil pointer. 887 // This pass removes a CHECKNIL if every predecessor path has already 888 // checked this value for nil. 889 // 890 // Simple backwards flood from check to definition. 891 // Run prog loop backward from end of program to beginning to avoid quadratic 892 // behavior removing a run of checks. 893 // 894 // Assume that stack variables with address not taken can be loaded multiple times 895 // from memory without being rechecked. Other variables need to be checked on 896 // each load. 897 898 var killed int // f->data is either nil or &killed 899 900 func nilopt(firstp *obj.Prog) { 901 g := Flowstart(firstp, nil) 902 if g == nil { 903 return 904 } 905 906 if Debug_checknil > 1 { /* || strcmp(curfn->nname->sym->name, "f1") == 0 */ 907 Dumpit("nilopt", g.Start, 0) 908 } 909 910 ncheck := 0 911 nkill := 0 912 var p *obj.Prog 913 for f := g.Start; f != nil; f = f.Link { 914 p = f.Prog 915 if p.As != obj.ACHECKNIL || !Thearch.Regtyp(&p.From) { 916 continue 917 } 918 ncheck++ 919 if Thearch.Stackaddr(&p.From) { 920 if Debug_checknil != 0 && p.Lineno > 1 { 921 Warnl(int(p.Lineno), "removed nil check of SP address") 922 } 923 f.Data = &killed 924 continue 925 } 926 927 nilwalkfwd(f) 928 if f.Data != nil { 929 if Debug_checknil != 0 && p.Lineno > 1 { 930 Warnl(int(p.Lineno), "removed nil check before indirect") 931 } 932 continue 933 } 934 935 nilwalkback(f) 936 if f.Data != nil { 937 if Debug_checknil != 0 && p.Lineno > 1 { 938 Warnl(int(p.Lineno), "removed repeated nil check") 939 } 940 continue 941 } 942 } 943 944 for f := g.Start; f != nil; f = f.Link { 945 if f.Data != nil { 946 nkill++ 947 Thearch.Excise(f) 948 } 949 } 950 951 Flowend(g) 952 953 if Debug_checknil > 1 { 954 fmt.Printf("%v: removed %d of %d nil checks\n", Curfn.Func.Nname.Sym, nkill, ncheck) 955 } 956 } 957 958 func nilwalkback(fcheck *Flow) { 959 for f := fcheck; f != nil; f = Uniqp(f) { 960 p := f.Prog 961 if (p.Info.Flags&RightWrite != 0) && Thearch.Sameaddr(&p.To, &fcheck.Prog.From) { 962 // Found initialization of value we're checking for nil. 963 // without first finding the check, so this one is unchecked. 964 return 965 } 966 967 if f != fcheck && p.As == obj.ACHECKNIL && Thearch.Sameaddr(&p.From, &fcheck.Prog.From) { 968 fcheck.Data = &killed 969 return 970 } 971 } 972 } 973 974 // Here is a more complex version that scans backward across branches. 975 // It assumes fcheck->kill = 1 has been set on entry, and its job is to find a reason 976 // to keep the check (setting fcheck->kill = 0). 977 // It doesn't handle copying of aggregates as well as I would like, 978 // nor variables with their address taken, 979 // and it's too subtle to turn on this late in Go 1.2. Perhaps for Go 1.3. 980 /* 981 for(f1 = f0; f1 != nil; f1 = f1->p1) { 982 if(f1->active == gen) 983 break; 984 f1->active = gen; 985 p = f1->prog; 986 987 // If same check, stop this loop but still check 988 // alternate predecessors up to this point. 989 if(f1 != fcheck && p->as == ACHECKNIL && thearch.sameaddr(&p->from, &fcheck->prog->from)) 990 break; 991 992 if((p.Info.flags & RightWrite) && thearch.sameaddr(&p->to, &fcheck->prog->from)) { 993 // Found initialization of value we're checking for nil. 994 // without first finding the check, so this one is unchecked. 995 fcheck->kill = 0; 996 return; 997 } 998 999 if(f1->p1 == nil && f1->p2 == nil) { 1000 print("lost pred for %v\n", fcheck->prog); 1001 for(f1=f0; f1!=nil; f1=f1->p1) { 1002 thearch.proginfo(&info, f1->prog); 1003 print("\t%v %d %d %D %D\n", r1->prog, info.flags&RightWrite, thearch.sameaddr(&f1->prog->to, &fcheck->prog->from), &f1->prog->to, &fcheck->prog->from); 1004 } 1005 fatal("lost pred trail"); 1006 } 1007 } 1008 1009 for(f = f0; f != f1; f = f->p1) 1010 for(f2 = f->p2; f2 != nil; f2 = f2->p2link) 1011 nilwalkback(fcheck, f2, gen); 1012 */ 1013 1014 func nilwalkfwd(fcheck *Flow) { 1015 // If the path down from rcheck dereferences the address 1016 // (possibly with a small offset) before writing to memory 1017 // and before any subsequent checks, it's okay to wait for 1018 // that implicit check. Only consider this basic block to 1019 // avoid problems like: 1020 // _ = *x // should panic 1021 // for {} // no writes but infinite loop may be considered visible 1022 1023 var last *Flow 1024 for f := Uniqs(fcheck); f != nil; f = Uniqs(f) { 1025 p := f.Prog 1026 if (p.Info.Flags&LeftRead != 0) && Thearch.Smallindir(&p.From, &fcheck.Prog.From) { 1027 fcheck.Data = &killed 1028 return 1029 } 1030 1031 if (p.Info.Flags&(RightRead|RightWrite) != 0) && Thearch.Smallindir(&p.To, &fcheck.Prog.From) { 1032 fcheck.Data = &killed 1033 return 1034 } 1035 1036 // Stop if another nil check happens. 1037 if p.As == obj.ACHECKNIL { 1038 return 1039 } 1040 1041 // Stop if value is lost. 1042 if (p.Info.Flags&RightWrite != 0) && Thearch.Sameaddr(&p.To, &fcheck.Prog.From) { 1043 return 1044 } 1045 1046 // Stop if memory write. 1047 if (p.Info.Flags&RightWrite != 0) && !Thearch.Regtyp(&p.To) { 1048 return 1049 } 1050 1051 // Stop if we jump backward. 1052 if last != nil && f.Id <= last.Id { 1053 return 1054 } 1055 last = f 1056 } 1057 }