github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/cmd/compile/internal/ssa/flagalloc.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  const flagRegMask = regMask(1) << 33 // TODO: arch-specific
     8  
     9  // flagalloc allocates the flag register among all the flag-generating
    10  // instructions. Flag values are recomputed if they need to be
    11  // spilled/restored.
    12  func flagalloc(f *Func) {
    13  	// Compute the in-register flag value we want at the end of
    14  	// each block. This is basically a best-effort live variable
    15  	// analysis, so it can be much simpler than a full analysis.
    16  	// TODO: do we really need to keep flag values live across blocks?
    17  	// Could we force the flags register to be unused at basic block
    18  	// boundaries?  Then we wouldn't need this computation.
    19  	end := make([]*Value, f.NumBlocks())
    20  	for n := 0; n < 2; n++ {
    21  		// Walk blocks backwards. Poor-man's postorder traversal.
    22  		for i := len(f.Blocks) - 1; i >= 0; i-- {
    23  			b := f.Blocks[i]
    24  			// Walk values backwards to figure out what flag
    25  			// value we want in the flag register at the start
    26  			// of the block.
    27  			flag := end[b.ID]
    28  			if b.Control != nil && b.Control.Type.IsFlags() {
    29  				flag = b.Control
    30  			}
    31  			for j := len(b.Values) - 1; j >= 0; j-- {
    32  				v := b.Values[j]
    33  				if v == flag {
    34  					flag = nil
    35  				}
    36  				if opcodeTable[v.Op].reg.clobbers&flagRegMask != 0 {
    37  					flag = nil
    38  				}
    39  				for _, a := range v.Args {
    40  					if a.Type.IsFlags() {
    41  						flag = a
    42  					}
    43  				}
    44  			}
    45  			if flag != nil {
    46  				for _, p := range b.Preds {
    47  					end[p.ID] = flag
    48  				}
    49  			}
    50  		}
    51  	}
    52  
    53  	// For blocks which have a flags control value, that's the only value
    54  	// we can leave in the flags register at the end of the block. (There
    55  	// is no place to put a flag regeneration instruction.)
    56  	for _, b := range f.Blocks {
    57  		v := b.Control
    58  		if v != nil && v.Type.IsFlags() && end[b.ID] != v {
    59  			end[b.ID] = nil
    60  		}
    61  		if b.Kind == BlockDefer {
    62  			// Defer blocks internally use/clobber the flags value.
    63  			end[b.ID] = nil
    64  		}
    65  	}
    66  
    67  	// Add flag recomputations where they are needed.
    68  	// TODO: Remove original instructions if they are never used.
    69  	var oldSched []*Value
    70  	for _, b := range f.Blocks {
    71  		oldSched = append(oldSched[:0], b.Values...)
    72  		b.Values = b.Values[:0]
    73  		// The current live flag value the pre-flagalloc copy).
    74  		var flag *Value
    75  		if len(b.Preds) > 0 {
    76  			flag = end[b.Preds[0].ID]
    77  			// Note: the following condition depends on the lack of critical edges.
    78  			for _, p := range b.Preds[1:] {
    79  				if end[p.ID] != flag {
    80  					f.Fatalf("live flag in %s's predecessors not consistent", b)
    81  				}
    82  			}
    83  		}
    84  		for _, v := range oldSched {
    85  			if v.Op == OpPhi && v.Type.IsFlags() {
    86  				f.Fatalf("phi of flags not supported: %s", v.LongString())
    87  			}
    88  			// Make sure any flag arg of v is in the flags register.
    89  			// If not, recompute it.
    90  			for i, a := range v.Args {
    91  				if !a.Type.IsFlags() {
    92  					continue
    93  				}
    94  				if a == flag {
    95  					continue
    96  				}
    97  				// Recalculate a
    98  				c := a.copyInto(b)
    99  				// Update v.
   100  				v.SetArg(i, c)
   101  				// Remember the most-recently computed flag value.
   102  				flag = a
   103  			}
   104  			// Issue v.
   105  			b.Values = append(b.Values, v)
   106  			if opcodeTable[v.Op].reg.clobbers&flagRegMask != 0 {
   107  				flag = nil
   108  			}
   109  			if v.Type.IsFlags() {
   110  				flag = v
   111  			}
   112  		}
   113  		if v := b.Control; v != nil && v != flag && v.Type.IsFlags() {
   114  			// Recalculate control value.
   115  			c := v.copyInto(b)
   116  			b.SetControl(c)
   117  			flag = v
   118  		}
   119  		if v := end[b.ID]; v != nil && v != flag {
   120  			// Need to reissue flag generator for use by
   121  			// subsequent blocks.
   122  			_ = v.copyInto(b)
   123  			// Note: this flag generator is not properly linked up
   124  			// with the flag users. This breaks the SSA representation.
   125  			// We could fix up the users with another pass, but for now
   126  			// we'll just leave it.  (Regalloc has the same issue for
   127  			// standard regs, and it runs next.)
   128  		}
   129  	}
   130  
   131  	// Save live flag state for later.
   132  	for _, b := range f.Blocks {
   133  		b.FlagsLiveAtEnd = end[b.ID] != nil
   134  	}
   135  }