github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/cmd/compile/internal/ssa/looprotate.go (about)

     1  // Copyright 2017 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  // loopRotate converts loops with a check-loop-condition-at-beginning
     8  // to loops with a check-loop-condition-at-end.
     9  // This helps loops avoid extra unnecessary jumps.
    10  //
    11  //   loop:
    12  //     CMPQ ...
    13  //     JGE exit
    14  //     ...
    15  //     JMP loop
    16  //   exit:
    17  //
    18  //    JMP entry
    19  //  loop:
    20  //    ...
    21  //  entry:
    22  //    CMPQ ...
    23  //    JLT loop
    24  func loopRotate(f *Func) {
    25  	loopnest := f.loopnest()
    26  	if len(loopnest.loops) == 0 {
    27  		return
    28  	}
    29  
    30  	idToIdx := make([]int, f.NumBlocks())
    31  	for i, b := range f.Blocks {
    32  		idToIdx[b.ID] = i
    33  	}
    34  
    35  	// Set of blocks we're moving, by ID.
    36  	move := map[ID]struct{}{}
    37  
    38  	// Map from block ID to the moving blocks that should
    39  	// come right after it.
    40  	after := map[ID][]*Block{}
    41  
    42  	// Check each loop header and decide if we want to move it.
    43  	for _, loop := range loopnest.loops {
    44  		b := loop.header
    45  		var p *Block // b's in-loop predecessor
    46  		for _, e := range b.Preds {
    47  			if e.b.Kind != BlockPlain {
    48  				continue
    49  			}
    50  			if loopnest.b2l[e.b.ID] != loop {
    51  				continue
    52  			}
    53  			p = e.b
    54  		}
    55  		if p == nil || p == b {
    56  			continue
    57  		}
    58  		after[p.ID] = []*Block{b}
    59  		for {
    60  			nextIdx := idToIdx[b.ID] + 1
    61  			if nextIdx >= len(f.Blocks) { // reached end of function (maybe impossible?)
    62  				break
    63  			}
    64  			nextb := f.Blocks[nextIdx]
    65  			if nextb == p { // original loop precedessor is next
    66  				break
    67  			}
    68  			if loopnest.b2l[nextb.ID] != loop { // about to leave loop
    69  				break
    70  			}
    71  			after[p.ID] = append(after[p.ID], nextb)
    72  			b = nextb
    73  		}
    74  
    75  		// Place b after p.
    76  		for _, b := range after[p.ID] {
    77  			move[b.ID] = struct{}{}
    78  		}
    79  	}
    80  
    81  	// Move blocks to their destinations in a single pass.
    82  	// We rely here on the fact that loop headers must come
    83  	// before the rest of the loop.  And that relies on the
    84  	// fact that we only identify reducible loops.
    85  	j := 0
    86  	for i, b := range f.Blocks {
    87  		if _, ok := move[b.ID]; ok {
    88  			continue
    89  		}
    90  		f.Blocks[j] = b
    91  		j++
    92  		for _, a := range after[b.ID] {
    93  			if j > i {
    94  				f.Fatalf("head before tail in loop %s", b)
    95  			}
    96  			f.Blocks[j] = a
    97  			j++
    98  		}
    99  	}
   100  	if j != len(f.Blocks) {
   101  		f.Fatalf("bad reordering in looprotate")
   102  	}
   103  }