github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/cmd/compile/internal/ssa/deadcode_test.go (about)

     1  // Copyright 2015 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 "testing"
     8  
     9  func TestDeadLoop(t *testing.T) {
    10  	c := testConfig(t)
    11  	fun := Fun(c, "entry",
    12  		Bloc("entry",
    13  			Valu("mem", OpInitMem, TypeMem, 0, nil),
    14  			Goto("exit")),
    15  		Bloc("exit",
    16  			Exit("mem")),
    17  		// dead loop
    18  		Bloc("deadblock",
    19  			// dead value in dead block
    20  			Valu("deadval", OpConstBool, TypeBool, 1, nil),
    21  			If("deadval", "deadblock", "exit")))
    22  
    23  	CheckFunc(fun.f)
    24  	Deadcode(fun.f)
    25  	CheckFunc(fun.f)
    26  
    27  	for _, b := range fun.f.Blocks {
    28  		if b == fun.blocks["deadblock"] {
    29  			t.Errorf("dead block not removed")
    30  		}
    31  		for _, v := range b.Values {
    32  			if v == fun.values["deadval"] {
    33  				t.Errorf("control value of dead block not removed")
    34  			}
    35  		}
    36  	}
    37  }
    38  
    39  func TestDeadValue(t *testing.T) {
    40  	c := testConfig(t)
    41  	fun := Fun(c, "entry",
    42  		Bloc("entry",
    43  			Valu("mem", OpInitMem, TypeMem, 0, nil),
    44  			Valu("deadval", OpConst64, TypeInt64, 37, nil),
    45  			Goto("exit")),
    46  		Bloc("exit",
    47  			Exit("mem")))
    48  
    49  	CheckFunc(fun.f)
    50  	Deadcode(fun.f)
    51  	CheckFunc(fun.f)
    52  
    53  	for _, b := range fun.f.Blocks {
    54  		for _, v := range b.Values {
    55  			if v == fun.values["deadval"] {
    56  				t.Errorf("dead value not removed")
    57  			}
    58  		}
    59  	}
    60  }
    61  
    62  func TestNeverTaken(t *testing.T) {
    63  	c := testConfig(t)
    64  	fun := Fun(c, "entry",
    65  		Bloc("entry",
    66  			Valu("cond", OpConstBool, TypeBool, 0, nil),
    67  			Valu("mem", OpInitMem, TypeMem, 0, nil),
    68  			If("cond", "then", "else")),
    69  		Bloc("then",
    70  			Goto("exit")),
    71  		Bloc("else",
    72  			Goto("exit")),
    73  		Bloc("exit",
    74  			Exit("mem")))
    75  
    76  	CheckFunc(fun.f)
    77  	Opt(fun.f)
    78  	Deadcode(fun.f)
    79  	CheckFunc(fun.f)
    80  
    81  	if fun.blocks["entry"].Kind != BlockPlain {
    82  		t.Errorf("if(false) not simplified")
    83  	}
    84  	for _, b := range fun.f.Blocks {
    85  		if b == fun.blocks["then"] {
    86  			t.Errorf("then block still present")
    87  		}
    88  		for _, v := range b.Values {
    89  			if v == fun.values["cond"] {
    90  				t.Errorf("constant condition still present")
    91  			}
    92  		}
    93  	}
    94  
    95  }
    96  
    97  func TestNestedDeadBlocks(t *testing.T) {
    98  	c := testConfig(t)
    99  	fun := Fun(c, "entry",
   100  		Bloc("entry",
   101  			Valu("mem", OpInitMem, TypeMem, 0, nil),
   102  			Valu("cond", OpConstBool, TypeBool, 0, nil),
   103  			If("cond", "b2", "b4")),
   104  		Bloc("b2",
   105  			If("cond", "b3", "b4")),
   106  		Bloc("b3",
   107  			If("cond", "b3", "b4")),
   108  		Bloc("b4",
   109  			If("cond", "b3", "exit")),
   110  		Bloc("exit",
   111  			Exit("mem")))
   112  
   113  	CheckFunc(fun.f)
   114  	Opt(fun.f)
   115  	CheckFunc(fun.f)
   116  	Deadcode(fun.f)
   117  	CheckFunc(fun.f)
   118  	if fun.blocks["entry"].Kind != BlockPlain {
   119  		t.Errorf("if(false) not simplified")
   120  	}
   121  	for _, b := range fun.f.Blocks {
   122  		if b == fun.blocks["b2"] {
   123  			t.Errorf("b2 block still present")
   124  		}
   125  		if b == fun.blocks["b3"] {
   126  			t.Errorf("b3 block still present")
   127  		}
   128  		for _, v := range b.Values {
   129  			if v == fun.values["cond"] {
   130  				t.Errorf("constant condition still present")
   131  			}
   132  		}
   133  	}
   134  }