Source file src/cmd/compile/internal/ssa/licm.go

     1  // Copyright 2016 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  // We are looking for loops with following structure
     8  // (loop bodies may have control flow inside):
     9  //
    10  //              +--------------+
    11  //              |              |
    12  //              |  preheader   |
    13  //              |              |
    14  //              +-------+------+
    15  //                      |
    16  //                      |
    17  //              +-------v------+
    18  //              |              |
    19  //       +------>    header    |
    20  //       |      |              |
    21  //       |      +-------+------+
    22  //       |              |
    23  //       |              |
    24  //       |      +-------v------+
    25  //       |      |              |
    26  //       +------+  loop body   |
    27  //              |              |
    28  //              +--------------+
    29  //
    30  //
    31  // We consider all phis and memory operations as initial loop dependent set.
    32  // So loop independent values are all loop values,
    33  // minus transitive closure of initial loop dependent values.
    34  // We remove those values from their BBs and move them to preheader.
    35  
    36  func licm(f *Func) {
    37  	// See likelyadjust.go for details about loop info.
    38  	nest := loopnestfor(f)
    39  	if len(nest.loops) == 0 || nest.hasIrreducible {
    40  		return
    41  	}
    42  
    43  	uses := uses(f)
    44  	defer uses.free(f)
    45  
    46  	loopDependent := f.Cache.allocBoolSlice(f.NumValues())
    47  	defer f.Cache.freeBoolSlice(loopDependent)
    48  	queue := f.Cache.allocValueSlice(f.NumValues())
    49  	defer f.Cache.freeValueSlice(queue)
    50  	queue = queue[:0]
    51  
    52  	// Start with all values we can't move out of loops.
    53  	for _, b := range f.Blocks {
    54  		if loop := nest.b2l[b.ID]; loop == nil || !loop.isInner {
    55  			// Values outside any loop we don't care about.
    56  			// Values not in a leaf loop we can't handle.
    57  			continue
    58  		}
    59  		for _, v := range b.Values {
    60  			if opcodeTable[v.Op].earlyOk {
    61  				// Double check we didn't mark the wrong ops as earlyOk
    62  				if v.Type.IsMemory() || opcodeTable[v.Op].nilCheck || opcodeTable[v.Op].hasSideEffects || v.MemoryArg() != nil {
    63  					v.Fatalf("op %s has bad earlyOk mark", v.Op)
    64  				}
    65  				if !v.Type.IsPtr() {
    66  					// Note: can't move pointer arithmetic, as it may be guarded by conditionals
    67  					// and thus could materialize a bad pointer across a safepoint.
    68  
    69  					continue // Ok to lift out of loop.
    70  				}
    71  			}
    72  			if v.Op == OpSelect0 || v.Op == OpSelect1 {
    73  				// These ops can (and must) move with the op they are selecting from.
    74  				continue
    75  			}
    76  			loopDependent[v.ID] = true
    77  			queue = append(queue, v)
    78  		}
    79  	}
    80  
    81  	// If a value can't be moved out of a loop, neither can its users.
    82  	// The queue contains values which are loop dependent, but their users
    83  	// have not been marked as loop dependent yet.
    84  	for len(queue) > 0 {
    85  		v := queue[len(queue)-1]
    86  		queue = queue[:len(queue)-1]
    87  
    88  		for _, u := range uses.get(v) {
    89  			if loop := nest.b2l[u.Block.ID]; loop == nil || !loop.isInner {
    90  				continue // see above
    91  			}
    92  			if loopDependent[u.ID] {
    93  				continue
    94  			}
    95  			loopDependent[u.ID] = true
    96  			queue = append(queue, u)
    97  		}
    98  	}
    99  
   100  	// Anything not marked as loop-dependent can be moved out of its loop.
   101  	for _, b := range f.Blocks {
   102  		loop := nest.b2l[b.ID]
   103  		if loop == nil || !loop.isInner {
   104  			// loopDependent check is wrong for loops containing other loops,
   105  			// because then a value might have an argument computed inside
   106  			// a nested loop.
   107  			continue
   108  		}
   109  		if len(loop.header.Preds) != 2 {
   110  			continue // is never true?
   111  		}
   112  		anyMoved := false
   113  		for i, v := range b.Values {
   114  			if loopDependent[v.ID] {
   115  				continue
   116  			}
   117  			// Figure out where to move loop-independent values.
   118  			h := loop.header
   119  			var inIdx int
   120  			if int(h.Preds[0].b.ID) >= len(nest.b2l) || nest.b2l[h.Preds[0].b.ID] != loop {
   121  				inIdx = 0
   122  			} else {
   123  				inIdx = 1
   124  			}
   125  			dest := h.Preds[inIdx].b
   126  			if dest.Kind != BlockPlain {
   127  				outIdx := h.Preds[inIdx].i
   128  				// Introduce a new block between the loop
   129  				// header predecessor and the loop header itself.
   130  				mid := f.NewBlock(BlockPlain)
   131  				mid.Pos = dest.Pos
   132  				// Splice into graph.
   133  				mid.Preds = append(mid.Preds, Edge{dest, outIdx})
   134  				mid.Succs = append(mid.Succs, Edge{h, inIdx})
   135  				h.Preds[inIdx] = Edge{mid, 0}
   136  				dest.Succs[outIdx] = Edge{mid, 0}
   137  
   138  				dest = mid
   139  			}
   140  
   141  			b.Values[i] = nil
   142  			v.Block = dest
   143  			dest.Values = append(dest.Values, v)
   144  			anyMoved = true
   145  		}
   146  		if anyMoved {
   147  			// We just nil'd entries in b.Values above. Compact out the nils.
   148  			i := 0
   149  			for _, v := range b.Values {
   150  				if v == nil {
   151  					continue
   152  				}
   153  				b.Values[i] = v
   154  				i++
   155  			}
   156  			b.Values = b.Values[:i]
   157  		}
   158  	}
   159  }
   160  

View as plain text