github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/engine/wazevo/ssa/basic_block_sort.go (about)

     1  //go:build go1.21
     2  
     3  package ssa
     4  
     5  import (
     6  	"slices"
     7  )
     8  
     9  func sortBlocks(blocks []*basicBlock) {
    10  	slices.SortFunc(blocks, func(i, j *basicBlock) int {
    11  		jIsReturn := j.ReturnBlock()
    12  		iIsReturn := i.ReturnBlock()
    13  		if iIsReturn && jIsReturn {
    14  			return 0
    15  		}
    16  		if jIsReturn {
    17  			return 1
    18  		}
    19  		if iIsReturn {
    20  			return -1
    21  		}
    22  		iRoot, jRoot := i.rootInstr, j.rootInstr
    23  		if iRoot == nil && jRoot == nil { // For testing.
    24  			return 0
    25  		}
    26  		if jRoot == nil {
    27  			return 1
    28  		}
    29  		if iRoot == nil {
    30  			return -1
    31  		}
    32  		return i.rootInstr.id - j.rootInstr.id
    33  	})
    34  }