Help us shape the future of Go! Take the 2026 Go Developer Survey

Source file src/cmd/compile/internal/ssacompile/known_bits_test.go

     1  // Copyright 2026 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 ssacompile
     6  
     7  import (
     8  	"testing"
     9  
    10  	"cmd/compile/internal/ssa"
    11  	"cmd/compile/internal/ssa/ssaop"
    12  	"cmd/compile/internal/types"
    13  )
    14  
    15  func TestRshCopiesSignBitEverywhere(t *testing.T) {
    16  	c := testConfig(t)
    17  
    18  	f := c.Fun("entry",
    19  		Bloc("entry",
    20  			Valu("mem", ssaop.OpInitMem, types.TypeMem, 0, nil),
    21  			Valu("x", ssaop.OpArg, c.config.Types.Int64, 0, nil),
    22  			Valu("y", ssaop.OpArg, c.config.Types.Int64, 1, nil),
    23  			Valu("ptr", ssaop.OpArg, c.config.Types.BytePtr, 2, nil),
    24  			Valu("signBit", ssaop.OpConst64, c.config.Types.Int64, -1<<63, nil),
    25  			Valu("xWithSignBitSet", ssaop.OpOr64, c.config.Types.Int64, 0, nil, "x", "signBit"),
    26  			Valu("oneHundredTwentyEight", ssaop.OpConst64, c.config.Types.Int64, 128, nil),
    27  			Valu("y", ssaop.OpOr64, c.config.Types.Int64, 0, nil, "y", "oneHundredTwentyEight"),
    28  			Valu("rsh", ssaop.OpRsh64x64, c.config.Types.Int64, 0, nil, "xWithSignBitSet", "y"),
    29  			Valu("one", ssaop.OpConst64, c.config.Types.Int64, 1, nil),
    30  			Valu("and", ssaop.OpAnd64, c.config.Types.Int64, 0, nil, "rsh", "one"),
    31  			Valu("mem", ssaop.OpStore, types.TypeMem, 0, nil, "ptr", "and", "mem"),
    32  			Exit("mem")))
    33  
    34  	CheckFunc(f.f)
    35  	ssa.KnownBits(f.f)
    36  	CheckFunc(f.f)
    37  
    38  	if len(f.f.Blocks) != 1 {
    39  		t.Fatalf("expected 1 block, got %d", len(f.f.Blocks))
    40  	}
    41  	var store *ssa.Value
    42  	for _, v := range f.f.Blocks[0].Values {
    43  		if v.Op == ssaop.OpStore {
    44  			if store != nil {
    45  				t.Fatalf("expected 1 store (%v), got a second one %v", store, v)
    46  			}
    47  			store = v
    48  		}
    49  	}
    50  	if store == nil {
    51  		t.Fatalf("expected 1 store, got none")
    52  	}
    53  
    54  	consT := store.Args[1]
    55  	for ; consT.Op == ssaop.OpCopy; consT = consT.Args[0] {
    56  	}
    57  
    58  	if consT.Op != ssaop.OpConst64 || consT.AuxInt != 1 {
    59  		t.Fatalf("expected store to have constant 1, got %v", consT.LongString())
    60  	}
    61  }
    62  

View as plain text