github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/compile/internal/ssa/func.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ssa 6 7 import ( 8 "github.com/gagliardetto/golang-go/cmd/compile/internal/types" 9 "github.com/gagliardetto/golang-go/cmd/internal/src" 10 "crypto/sha1" 11 "fmt" 12 "io" 13 "math" 14 "os" 15 "strings" 16 ) 17 18 type writeSyncer interface { 19 io.Writer 20 Sync() error 21 } 22 23 // A Func represents a Go func declaration (or function literal) and its body. 24 // This package compiles each Func independently. 25 // Funcs are single-use; a new Func must be created for every compiled function. 26 type Func struct { 27 Config *Config // architecture information 28 Cache *Cache // re-usable cache 29 fe Frontend // frontend state associated with this Func, callbacks into compiler frontend 30 pass *pass // current pass information (name, options, etc.) 31 Name string // e.g. NewFunc or (*Func).NumBlocks (no package prefix) 32 Type *types.Type // type signature of the function. 33 Blocks []*Block // unordered set of all basic blocks (note: not indexable by ID) 34 Entry *Block // the entry basic block 35 36 // If we are using open-coded defers, this is the first call to a deferred 37 // function in the final defer exit sequence that we generated. This call 38 // should be after all defer statements, and will have all args, etc. of 39 // all defer calls as live. The liveness info of this call will be used 40 // for the deferreturn/ret segment generated for functions with open-coded 41 // defers. 42 LastDeferExit *Value 43 bid idAlloc // block ID allocator 44 vid idAlloc // value ID allocator 45 46 // Given an environment variable used for debug hash match, 47 // what file (if any) receives the yes/no logging? 48 logfiles map[string]writeSyncer 49 HTMLWriter *HTMLWriter // html writer, for debugging 50 DebugTest bool // default true unless $GOSSAHASH != ""; as a debugging aid, make new code conditional on this and use GOSSAHASH to binary search for failing cases 51 PrintOrHtmlSSA bool // true if GOSSAFUNC matches, true even if fe.Log() (spew phase results to stdout) is false. 52 ruleMatches map[string]int // number of times countRule was called during compilation for any given string 53 54 scheduled bool // Values in Blocks are in final order 55 laidout bool // Blocks are ordered 56 NoSplit bool // true if function is marked as nosplit. Used by schedule check pass. 57 58 // when register allocation is done, maps value ids to locations 59 RegAlloc []Location 60 61 // map from LocalSlot to set of Values that we want to store in that slot. 62 NamedValues map[LocalSlot][]*Value 63 // Names is a copy of NamedValues.Keys. We keep a separate list 64 // of keys to make iteration order deterministic. 65 Names []LocalSlot 66 67 // WBLoads is a list of Blocks that branch on the write 68 // barrier flag. Safe-points are disabled from the OpLoad that 69 // reads the write-barrier flag until the control flow rejoins 70 // below the two successors of this block. 71 WBLoads []*Block 72 73 freeValues *Value // free Values linked by argstorage[0]. All other fields except ID are 0/nil. 74 freeBlocks *Block // free Blocks linked by succstorage[0].b. All other fields except ID are 0/nil. 75 76 cachedPostorder []*Block // cached postorder traversal 77 cachedIdom []*Block // cached immediate dominators 78 cachedSdom SparseTree // cached dominator tree 79 cachedLoopnest *loopnest // cached loop nest information 80 cachedLineStarts *xposmap // cached map/set of xpos to integers 81 82 auxmap auxmap // map from aux values to opaque ids used by CSE 83 constants map[int64][]*Value // constants cache, keyed by constant value; users must check value's Op and Type 84 } 85 86 // NewFunc returns a new, empty function object. 87 // Caller must set f.Config and f.Cache before using f. 88 func NewFunc(fe Frontend) *Func { 89 return &Func{fe: fe, NamedValues: make(map[LocalSlot][]*Value)} 90 } 91 92 // NumBlocks returns an integer larger than the id of any Block in the Func. 93 func (f *Func) NumBlocks() int { 94 return f.bid.num() 95 } 96 97 // NumValues returns an integer larger than the id of any Value in the Func. 98 func (f *Func) NumValues() int { 99 return f.vid.num() 100 } 101 102 // newSparseSet returns a sparse set that can store at least up to n integers. 103 func (f *Func) newSparseSet(n int) *sparseSet { 104 for i, scr := range f.Cache.scrSparseSet { 105 if scr != nil && scr.cap() >= n { 106 f.Cache.scrSparseSet[i] = nil 107 scr.clear() 108 return scr 109 } 110 } 111 return newSparseSet(n) 112 } 113 114 // retSparseSet returns a sparse set to the config's cache of sparse 115 // sets to be reused by f.newSparseSet. 116 func (f *Func) retSparseSet(ss *sparseSet) { 117 for i, scr := range f.Cache.scrSparseSet { 118 if scr == nil { 119 f.Cache.scrSparseSet[i] = ss 120 return 121 } 122 } 123 f.Cache.scrSparseSet = append(f.Cache.scrSparseSet, ss) 124 } 125 126 // newSparseMap returns a sparse map that can store at least up to n integers. 127 func (f *Func) newSparseMap(n int) *sparseMap { 128 for i, scr := range f.Cache.scrSparseMap { 129 if scr != nil && scr.cap() >= n { 130 f.Cache.scrSparseMap[i] = nil 131 scr.clear() 132 return scr 133 } 134 } 135 return newSparseMap(n) 136 } 137 138 // retSparseMap returns a sparse map to the config's cache of sparse 139 // sets to be reused by f.newSparseMap. 140 func (f *Func) retSparseMap(ss *sparseMap) { 141 for i, scr := range f.Cache.scrSparseMap { 142 if scr == nil { 143 f.Cache.scrSparseMap[i] = ss 144 return 145 } 146 } 147 f.Cache.scrSparseMap = append(f.Cache.scrSparseMap, ss) 148 } 149 150 // newPoset returns a new poset from the internal cache 151 func (f *Func) newPoset() *poset { 152 if len(f.Cache.scrPoset) > 0 { 153 po := f.Cache.scrPoset[len(f.Cache.scrPoset)-1] 154 f.Cache.scrPoset = f.Cache.scrPoset[:len(f.Cache.scrPoset)-1] 155 return po 156 } 157 return newPoset() 158 } 159 160 // retPoset returns a poset to the internal cache 161 func (f *Func) retPoset(po *poset) { 162 f.Cache.scrPoset = append(f.Cache.scrPoset, po) 163 } 164 165 // newDeadcodeLive returns a slice for the 166 // deadcode pass to use to indicate which values are live. 167 func (f *Func) newDeadcodeLive() []bool { 168 r := f.Cache.deadcode.live 169 f.Cache.deadcode.live = nil 170 return r 171 } 172 173 // retDeadcodeLive returns a deadcode live value slice for re-use. 174 func (f *Func) retDeadcodeLive(live []bool) { 175 f.Cache.deadcode.live = live 176 } 177 178 // newDeadcodeLiveOrderStmts returns a slice for the 179 // deadcode pass to use to indicate which values 180 // need special treatment for statement boundaries. 181 func (f *Func) newDeadcodeLiveOrderStmts() []*Value { 182 r := f.Cache.deadcode.liveOrderStmts 183 f.Cache.deadcode.liveOrderStmts = nil 184 return r 185 } 186 187 // retDeadcodeLiveOrderStmts returns a deadcode liveOrderStmts slice for re-use. 188 func (f *Func) retDeadcodeLiveOrderStmts(liveOrderStmts []*Value) { 189 f.Cache.deadcode.liveOrderStmts = liveOrderStmts 190 } 191 192 // newValue allocates a new Value with the given fields and places it at the end of b.Values. 193 func (f *Func) newValue(op Op, t *types.Type, b *Block, pos src.XPos) *Value { 194 var v *Value 195 if f.freeValues != nil { 196 v = f.freeValues 197 f.freeValues = v.argstorage[0] 198 v.argstorage[0] = nil 199 } else { 200 ID := f.vid.get() 201 if int(ID) < len(f.Cache.values) { 202 v = &f.Cache.values[ID] 203 v.ID = ID 204 } else { 205 v = &Value{ID: ID} 206 } 207 } 208 v.Op = op 209 v.Type = t 210 v.Block = b 211 if notStmtBoundary(op) { 212 pos = pos.WithNotStmt() 213 } 214 v.Pos = pos 215 b.Values = append(b.Values, v) 216 return v 217 } 218 219 // newValueNoBlock allocates a new Value with the given fields. 220 // The returned value is not placed in any block. Once the caller 221 // decides on a block b, it must set b.Block and append 222 // the returned value to b.Values. 223 func (f *Func) newValueNoBlock(op Op, t *types.Type, pos src.XPos) *Value { 224 var v *Value 225 if f.freeValues != nil { 226 v = f.freeValues 227 f.freeValues = v.argstorage[0] 228 v.argstorage[0] = nil 229 } else { 230 ID := f.vid.get() 231 if int(ID) < len(f.Cache.values) { 232 v = &f.Cache.values[ID] 233 v.ID = ID 234 } else { 235 v = &Value{ID: ID} 236 } 237 } 238 v.Op = op 239 v.Type = t 240 v.Block = nil // caller must fix this. 241 if notStmtBoundary(op) { 242 pos = pos.WithNotStmt() 243 } 244 v.Pos = pos 245 return v 246 } 247 248 // logPassStat writes a string key and int value as a warning in a 249 // tab-separated format easily handled by spreadsheets or awk. 250 // file names, lines, and function names are included to provide enough (?) 251 // context to allow item-by-item comparisons across runs. 252 // For example: 253 // awk 'BEGIN {FS="\t"} $3~/TIME/{sum+=$4} END{print "t(ns)=",sum}' t.log 254 func (f *Func) LogStat(key string, args ...interface{}) { 255 value := "" 256 for _, a := range args { 257 value += fmt.Sprintf("\t%v", a) 258 } 259 n := "missing_pass" 260 if f.pass != nil { 261 n = strings.Replace(f.pass.name, " ", "_", -1) 262 } 263 f.Warnl(f.Entry.Pos, "\t%s\t%s%s\t%s", n, key, value, f.Name) 264 } 265 266 // freeValue frees a value. It must no longer be referenced or have any args. 267 func (f *Func) freeValue(v *Value) { 268 if v.Block == nil { 269 f.Fatalf("trying to free an already freed value") 270 } 271 if v.Uses != 0 { 272 f.Fatalf("value %s still has %d uses", v, v.Uses) 273 } 274 if len(v.Args) != 0 { 275 f.Fatalf("value %s still has %d args", v, len(v.Args)) 276 } 277 // Clear everything but ID (which we reuse). 278 id := v.ID 279 280 // Values with zero arguments and OpOffPtr values might be cached, so remove them there. 281 nArgs := opcodeTable[v.Op].argLen 282 if nArgs == 0 || v.Op == OpOffPtr { 283 vv := f.constants[v.AuxInt] 284 for i, cv := range vv { 285 if v == cv { 286 vv[i] = vv[len(vv)-1] 287 vv[len(vv)-1] = nil 288 f.constants[v.AuxInt] = vv[0 : len(vv)-1] 289 break 290 } 291 } 292 } 293 *v = Value{} 294 v.ID = id 295 v.argstorage[0] = f.freeValues 296 f.freeValues = v 297 } 298 299 // newBlock allocates a new Block of the given kind and places it at the end of f.Blocks. 300 func (f *Func) NewBlock(kind BlockKind) *Block { 301 var b *Block 302 if f.freeBlocks != nil { 303 b = f.freeBlocks 304 f.freeBlocks = b.succstorage[0].b 305 b.succstorage[0].b = nil 306 } else { 307 ID := f.bid.get() 308 if int(ID) < len(f.Cache.blocks) { 309 b = &f.Cache.blocks[ID] 310 b.ID = ID 311 } else { 312 b = &Block{ID: ID} 313 } 314 } 315 b.Kind = kind 316 b.Func = f 317 b.Preds = b.predstorage[:0] 318 b.Succs = b.succstorage[:0] 319 b.Values = b.valstorage[:0] 320 f.Blocks = append(f.Blocks, b) 321 f.invalidateCFG() 322 return b 323 } 324 325 func (f *Func) freeBlock(b *Block) { 326 if b.Func == nil { 327 f.Fatalf("trying to free an already freed block") 328 } 329 // Clear everything but ID (which we reuse). 330 id := b.ID 331 *b = Block{} 332 b.ID = id 333 b.succstorage[0].b = f.freeBlocks 334 f.freeBlocks = b 335 } 336 337 // NewValue0 returns a new value in the block with no arguments and zero aux values. 338 func (b *Block) NewValue0(pos src.XPos, op Op, t *types.Type) *Value { 339 v := b.Func.newValue(op, t, b, pos) 340 v.AuxInt = 0 341 v.Args = v.argstorage[:0] 342 return v 343 } 344 345 // NewValue returns a new value in the block with no arguments and an auxint value. 346 func (b *Block) NewValue0I(pos src.XPos, op Op, t *types.Type, auxint int64) *Value { 347 v := b.Func.newValue(op, t, b, pos) 348 v.AuxInt = auxint 349 v.Args = v.argstorage[:0] 350 return v 351 } 352 353 // NewValue returns a new value in the block with no arguments and an aux value. 354 func (b *Block) NewValue0A(pos src.XPos, op Op, t *types.Type, aux interface{}) *Value { 355 if _, ok := aux.(int64); ok { 356 // Disallow int64 aux values. They should be in the auxint field instead. 357 // Maybe we want to allow this at some point, but for now we disallow it 358 // to prevent errors like using NewValue1A instead of NewValue1I. 359 b.Fatalf("aux field has int64 type op=%s type=%s aux=%v", op, t, aux) 360 } 361 v := b.Func.newValue(op, t, b, pos) 362 v.AuxInt = 0 363 v.Aux = aux 364 v.Args = v.argstorage[:0] 365 return v 366 } 367 368 // NewValue returns a new value in the block with no arguments and both an auxint and aux values. 369 func (b *Block) NewValue0IA(pos src.XPos, op Op, t *types.Type, auxint int64, aux interface{}) *Value { 370 v := b.Func.newValue(op, t, b, pos) 371 v.AuxInt = auxint 372 v.Aux = aux 373 v.Args = v.argstorage[:0] 374 return v 375 } 376 377 // NewValue1 returns a new value in the block with one argument and zero aux values. 378 func (b *Block) NewValue1(pos src.XPos, op Op, t *types.Type, arg *Value) *Value { 379 v := b.Func.newValue(op, t, b, pos) 380 v.AuxInt = 0 381 v.Args = v.argstorage[:1] 382 v.argstorage[0] = arg 383 arg.Uses++ 384 return v 385 } 386 387 // NewValue1I returns a new value in the block with one argument and an auxint value. 388 func (b *Block) NewValue1I(pos src.XPos, op Op, t *types.Type, auxint int64, arg *Value) *Value { 389 v := b.Func.newValue(op, t, b, pos) 390 v.AuxInt = auxint 391 v.Args = v.argstorage[:1] 392 v.argstorage[0] = arg 393 arg.Uses++ 394 return v 395 } 396 397 // NewValue1A returns a new value in the block with one argument and an aux value. 398 func (b *Block) NewValue1A(pos src.XPos, op Op, t *types.Type, aux interface{}, arg *Value) *Value { 399 v := b.Func.newValue(op, t, b, pos) 400 v.AuxInt = 0 401 v.Aux = aux 402 v.Args = v.argstorage[:1] 403 v.argstorage[0] = arg 404 arg.Uses++ 405 return v 406 } 407 408 // NewValue1IA returns a new value in the block with one argument and both an auxint and aux values. 409 func (b *Block) NewValue1IA(pos src.XPos, op Op, t *types.Type, auxint int64, aux interface{}, arg *Value) *Value { 410 v := b.Func.newValue(op, t, b, pos) 411 v.AuxInt = auxint 412 v.Aux = aux 413 v.Args = v.argstorage[:1] 414 v.argstorage[0] = arg 415 arg.Uses++ 416 return v 417 } 418 419 // NewValue2 returns a new value in the block with two arguments and zero aux values. 420 func (b *Block) NewValue2(pos src.XPos, op Op, t *types.Type, arg0, arg1 *Value) *Value { 421 v := b.Func.newValue(op, t, b, pos) 422 v.AuxInt = 0 423 v.Args = v.argstorage[:2] 424 v.argstorage[0] = arg0 425 v.argstorage[1] = arg1 426 arg0.Uses++ 427 arg1.Uses++ 428 return v 429 } 430 431 // NewValue2A returns a new value in the block with two arguments and one aux values. 432 func (b *Block) NewValue2A(pos src.XPos, op Op, t *types.Type, aux interface{}, arg0, arg1 *Value) *Value { 433 v := b.Func.newValue(op, t, b, pos) 434 v.AuxInt = 0 435 v.Aux = aux 436 v.Args = v.argstorage[:2] 437 v.argstorage[0] = arg0 438 v.argstorage[1] = arg1 439 arg0.Uses++ 440 arg1.Uses++ 441 return v 442 } 443 444 // NewValue2I returns a new value in the block with two arguments and an auxint value. 445 func (b *Block) NewValue2I(pos src.XPos, op Op, t *types.Type, auxint int64, arg0, arg1 *Value) *Value { 446 v := b.Func.newValue(op, t, b, pos) 447 v.AuxInt = auxint 448 v.Args = v.argstorage[:2] 449 v.argstorage[0] = arg0 450 v.argstorage[1] = arg1 451 arg0.Uses++ 452 arg1.Uses++ 453 return v 454 } 455 456 // NewValue2IA returns a new value in the block with two arguments and both an auxint and aux values. 457 func (b *Block) NewValue2IA(pos src.XPos, op Op, t *types.Type, auxint int64, aux interface{}, arg0, arg1 *Value) *Value { 458 v := b.Func.newValue(op, t, b, pos) 459 v.AuxInt = auxint 460 v.Aux = aux 461 v.Args = v.argstorage[:2] 462 v.argstorage[0] = arg0 463 v.argstorage[1] = arg1 464 arg0.Uses++ 465 arg1.Uses++ 466 return v 467 } 468 469 // NewValue3 returns a new value in the block with three arguments and zero aux values. 470 func (b *Block) NewValue3(pos src.XPos, op Op, t *types.Type, arg0, arg1, arg2 *Value) *Value { 471 v := b.Func.newValue(op, t, b, pos) 472 v.AuxInt = 0 473 v.Args = v.argstorage[:3] 474 v.argstorage[0] = arg0 475 v.argstorage[1] = arg1 476 v.argstorage[2] = arg2 477 arg0.Uses++ 478 arg1.Uses++ 479 arg2.Uses++ 480 return v 481 } 482 483 // NewValue3I returns a new value in the block with three arguments and an auxint value. 484 func (b *Block) NewValue3I(pos src.XPos, op Op, t *types.Type, auxint int64, arg0, arg1, arg2 *Value) *Value { 485 v := b.Func.newValue(op, t, b, pos) 486 v.AuxInt = auxint 487 v.Args = v.argstorage[:3] 488 v.argstorage[0] = arg0 489 v.argstorage[1] = arg1 490 v.argstorage[2] = arg2 491 arg0.Uses++ 492 arg1.Uses++ 493 arg2.Uses++ 494 return v 495 } 496 497 // NewValue3A returns a new value in the block with three argument and an aux value. 498 func (b *Block) NewValue3A(pos src.XPos, op Op, t *types.Type, aux interface{}, arg0, arg1, arg2 *Value) *Value { 499 v := b.Func.newValue(op, t, b, pos) 500 v.AuxInt = 0 501 v.Aux = aux 502 v.Args = v.argstorage[:3] 503 v.argstorage[0] = arg0 504 v.argstorage[1] = arg1 505 v.argstorage[2] = arg2 506 arg0.Uses++ 507 arg1.Uses++ 508 arg2.Uses++ 509 return v 510 } 511 512 // NewValue4 returns a new value in the block with four arguments and zero aux values. 513 func (b *Block) NewValue4(pos src.XPos, op Op, t *types.Type, arg0, arg1, arg2, arg3 *Value) *Value { 514 v := b.Func.newValue(op, t, b, pos) 515 v.AuxInt = 0 516 v.Args = []*Value{arg0, arg1, arg2, arg3} 517 arg0.Uses++ 518 arg1.Uses++ 519 arg2.Uses++ 520 arg3.Uses++ 521 return v 522 } 523 524 // NewValue4I returns a new value in the block with four arguments and and auxint value. 525 func (b *Block) NewValue4I(pos src.XPos, op Op, t *types.Type, auxint int64, arg0, arg1, arg2, arg3 *Value) *Value { 526 v := b.Func.newValue(op, t, b, pos) 527 v.AuxInt = auxint 528 v.Args = []*Value{arg0, arg1, arg2, arg3} 529 arg0.Uses++ 530 arg1.Uses++ 531 arg2.Uses++ 532 arg3.Uses++ 533 return v 534 } 535 536 // constVal returns a constant value for c. 537 func (f *Func) constVal(op Op, t *types.Type, c int64, setAuxInt bool) *Value { 538 if f.constants == nil { 539 f.constants = make(map[int64][]*Value) 540 } 541 vv := f.constants[c] 542 for _, v := range vv { 543 if v.Op == op && v.Type.Compare(t) == types.CMPeq { 544 if setAuxInt && v.AuxInt != c { 545 panic(fmt.Sprintf("cached const %s should have AuxInt of %d", v.LongString(), c)) 546 } 547 return v 548 } 549 } 550 var v *Value 551 if setAuxInt { 552 v = f.Entry.NewValue0I(src.NoXPos, op, t, c) 553 } else { 554 v = f.Entry.NewValue0(src.NoXPos, op, t) 555 } 556 f.constants[c] = append(vv, v) 557 return v 558 } 559 560 // These magic auxint values let us easily cache non-numeric constants 561 // using the same constants map while making collisions unlikely. 562 // These values are unlikely to occur in regular code and 563 // are easy to grep for in case of bugs. 564 const ( 565 constSliceMagic = 1122334455 566 constInterfaceMagic = 2233445566 567 constNilMagic = 3344556677 568 constEmptyStringMagic = 4455667788 569 ) 570 571 // ConstInt returns an int constant representing its argument. 572 func (f *Func) ConstBool(t *types.Type, c bool) *Value { 573 i := int64(0) 574 if c { 575 i = 1 576 } 577 return f.constVal(OpConstBool, t, i, true) 578 } 579 func (f *Func) ConstInt8(t *types.Type, c int8) *Value { 580 return f.constVal(OpConst8, t, int64(c), true) 581 } 582 func (f *Func) ConstInt16(t *types.Type, c int16) *Value { 583 return f.constVal(OpConst16, t, int64(c), true) 584 } 585 func (f *Func) ConstInt32(t *types.Type, c int32) *Value { 586 return f.constVal(OpConst32, t, int64(c), true) 587 } 588 func (f *Func) ConstInt64(t *types.Type, c int64) *Value { 589 return f.constVal(OpConst64, t, c, true) 590 } 591 func (f *Func) ConstFloat32(t *types.Type, c float64) *Value { 592 return f.constVal(OpConst32F, t, int64(math.Float64bits(float64(float32(c)))), true) 593 } 594 func (f *Func) ConstFloat64(t *types.Type, c float64) *Value { 595 return f.constVal(OpConst64F, t, int64(math.Float64bits(c)), true) 596 } 597 598 func (f *Func) ConstSlice(t *types.Type) *Value { 599 return f.constVal(OpConstSlice, t, constSliceMagic, false) 600 } 601 func (f *Func) ConstInterface(t *types.Type) *Value { 602 return f.constVal(OpConstInterface, t, constInterfaceMagic, false) 603 } 604 func (f *Func) ConstNil(t *types.Type) *Value { 605 return f.constVal(OpConstNil, t, constNilMagic, false) 606 } 607 func (f *Func) ConstEmptyString(t *types.Type) *Value { 608 v := f.constVal(OpConstString, t, constEmptyStringMagic, false) 609 v.Aux = "" 610 return v 611 } 612 func (f *Func) ConstOffPtrSP(t *types.Type, c int64, sp *Value) *Value { 613 v := f.constVal(OpOffPtr, t, c, true) 614 if len(v.Args) == 0 { 615 v.AddArg(sp) 616 } 617 return v 618 619 } 620 621 func (f *Func) Frontend() Frontend { return f.fe } 622 func (f *Func) Warnl(pos src.XPos, msg string, args ...interface{}) { f.fe.Warnl(pos, msg, args...) } 623 func (f *Func) Logf(msg string, args ...interface{}) { f.fe.Logf(msg, args...) } 624 func (f *Func) Log() bool { return f.fe.Log() } 625 func (f *Func) Fatalf(msg string, args ...interface{}) { f.fe.Fatalf(f.Entry.Pos, msg, args...) } 626 627 // postorder returns the reachable blocks in f in a postorder traversal. 628 func (f *Func) postorder() []*Block { 629 if f.cachedPostorder == nil { 630 f.cachedPostorder = postorder(f) 631 } 632 return f.cachedPostorder 633 } 634 635 func (f *Func) Postorder() []*Block { 636 return f.postorder() 637 } 638 639 // Idom returns a map from block ID to the immediate dominator of that block. 640 // f.Entry.ID maps to nil. Unreachable blocks map to nil as well. 641 func (f *Func) Idom() []*Block { 642 if f.cachedIdom == nil { 643 f.cachedIdom = dominators(f) 644 } 645 return f.cachedIdom 646 } 647 648 // sdom returns a sparse tree representing the dominator relationships 649 // among the blocks of f. 650 func (f *Func) Sdom() SparseTree { 651 if f.cachedSdom == nil { 652 f.cachedSdom = newSparseTree(f, f.Idom()) 653 } 654 return f.cachedSdom 655 } 656 657 // loopnest returns the loop nest information for f. 658 func (f *Func) loopnest() *loopnest { 659 if f.cachedLoopnest == nil { 660 f.cachedLoopnest = loopnestfor(f) 661 } 662 return f.cachedLoopnest 663 } 664 665 // invalidateCFG tells f that its CFG has changed. 666 func (f *Func) invalidateCFG() { 667 f.cachedPostorder = nil 668 f.cachedIdom = nil 669 f.cachedSdom = nil 670 f.cachedLoopnest = nil 671 } 672 673 // DebugHashMatch reports whether environment variable evname 674 // 1) is empty (this is a special more-quickly implemented case of 3) 675 // 2) is "y" or "Y" 676 // 3) is a suffix of the sha1 hash of name 677 // 4) is a suffix of the environment variable 678 // fmt.Sprintf("%s%d", evname, n) 679 // provided that all such variables are nonempty for 0 <= i <= n 680 // Otherwise it returns false. 681 // When true is returned the message 682 // "%s triggered %s\n", evname, name 683 // is printed on the file named in environment variable 684 // GSHS_LOGFILE 685 // or standard out if that is empty or there is an error 686 // opening the file. 687 func (f *Func) DebugHashMatch(evname, name string) bool { 688 evhash := os.Getenv(evname) 689 switch evhash { 690 case "": 691 return true // default behavior with no EV is "on" 692 case "y", "Y": 693 f.logDebugHashMatch(evname, name) 694 return true 695 case "n", "N": 696 return false 697 } 698 // Check the hash of the name against a partial input hash. 699 // We use this feature to do a binary search to 700 // find a function that is incorrectly compiled. 701 hstr := "" 702 for _, b := range sha1.Sum([]byte(name)) { 703 hstr += fmt.Sprintf("%08b", b) 704 } 705 706 if strings.HasSuffix(hstr, evhash) { 707 f.logDebugHashMatch(evname, name) 708 return true 709 } 710 711 // Iteratively try additional hashes to allow tests for multi-point 712 // failure. 713 for i := 0; true; i++ { 714 ev := fmt.Sprintf("%s%d", evname, i) 715 evv := os.Getenv(ev) 716 if evv == "" { 717 break 718 } 719 if strings.HasSuffix(hstr, evv) { 720 f.logDebugHashMatch(ev, name) 721 return true 722 } 723 } 724 return false 725 } 726 727 func (f *Func) logDebugHashMatch(evname, name string) { 728 if f.logfiles == nil { 729 f.logfiles = make(map[string]writeSyncer) 730 } 731 file := f.logfiles[evname] 732 if file == nil { 733 file = os.Stdout 734 if tmpfile := os.Getenv("GSHS_LOGFILE"); tmpfile != "" { 735 var err error 736 file, err = os.Create(tmpfile) 737 if err != nil { 738 f.Fatalf("could not open hash-testing logfile %s", tmpfile) 739 } 740 } 741 f.logfiles[evname] = file 742 } 743 fmt.Fprintf(file, "%s triggered %s\n", evname, name) 744 file.Sync() 745 } 746 747 func DebugNameMatch(evname, name string) bool { 748 return os.Getenv(evname) == name 749 }