Source file src/cmd/compile/internal/ssa/phiopt_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 ssa
     6  
     7  import (
     8  	"testing"
     9  
    10  	"cmd/compile/internal/types"
    11  )
    12  
    13  func TestPhioptSpecialCase(t *testing.T) {
    14  	for _, layout := range []struct {
    15  		name         string
    16  		reversePreds bool
    17  		directTrue   bool
    18  	}{
    19  		{"pred0/true", false, true},
    20  		{"pred0/false", false, false},
    21  		{"pred1/true", true, true},
    22  		{"pred1/false", true, false},
    23  	} {
    24  		for _, cyclic := range []bool{false, true} {
    25  			name := "acyclic/"
    26  			if cyclic {
    27  				name = "loop/"
    28  			}
    29  			t.Run(name+layout.name, func(t *testing.T) {
    30  				c := testConfig(t)
    31  				boolType := c.config.Types.Bool
    32  				succ0, succ1 := "merge", "other"
    33  				if !layout.directTrue {
    34  					succ0, succ1 = succ1, succ0
    35  				}
    36  				arg0, arg1 := "false", "true"
    37  				if layout.reversePreds {
    38  					arg0, arg1 = arg1, arg0
    39  				}
    40  				mergeSucc := "done"
    41  				if cyclic {
    42  					mergeSucc = "other"
    43  				}
    44  				blocks := []bloc{
    45  					Bloc("entry",
    46  						Valu("mem", OpInitMem, types.TypeMem, 0, nil),
    47  						Valu("cond", OpArg, boolType, 0, nil),
    48  						Valu("cond2", OpArg, boolType, 0, nil),
    49  						Valu("false", OpConstBool, boolType, 0, nil),
    50  						Valu("true", OpConstBool, boolType, 1, nil),
    51  						If("cond", succ0, succ1)),
    52  					Bloc("other", If("cond2", "back", "exit")),
    53  					Bloc("back", Goto("merge")),
    54  					Bloc("merge",
    55  						Valu("phi", OpPhi, boolType, 0, nil, arg0, arg1),
    56  						If("phi", mergeSucc, "exit")),
    57  					Bloc("exit", Exit("mem")),
    58  				}
    59  				if !cyclic {
    60  					blocks = append(blocks, Bloc("done", Exit("mem")))
    61  				}
    62  				if layout.reversePreds {
    63  					blocks[0], blocks[2] = blocks[2], blocks[0]
    64  				}
    65  				fun := c.Fun("entry", blocks...)
    66  
    67  				// The other successor dominates back in both graphs. In the
    68  				// cyclic graph it can also be reached through merge, so cond
    69  				// does not determine which predecessor supplies phi's value.
    70  				CheckFunc(fun.f)
    71  				phiopt(fun.f)
    72  				CheckFunc(fun.f)
    73  
    74  				v := fun.values["phi"]
    75  				want := OpCopy
    76  				if layout.directTrue {
    77  					want = OpNot
    78  				}
    79  				if cyclic {
    80  					want = OpPhi
    81  				}
    82  				if v.Op != want {
    83  					t.Fatalf("phi became %s, want %s", v.LongString(), want)
    84  				}
    85  				if cyclic {
    86  					if v.Args[0] != fun.values[arg0] || v.Args[1] != fun.values[arg1] {
    87  						t.Fatalf("phi arguments changed: %s", v.LongString())
    88  					}
    89  				} else if len(v.Args) != 1 || v.Args[0] != fun.values["cond"] {
    90  					t.Fatalf("phi should use entry condition: %s", v.LongString())
    91  				}
    92  			})
    93  		}
    94  	}
    95  }
    96  

View as plain text