1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <gtest/gtest.h>
24 #include "nir.h"
25 #include "nir_builder.h"
26
27 class nir_cf_test : public ::testing::Test {
28 protected:
29 nir_cf_test();
30 ~nir_cf_test();
31
32 nir_builder b;
33 };
34
nir_cf_test()35 nir_cf_test::nir_cf_test()
36 {
37 glsl_type_singleton_init_or_ref();
38
39 static const nir_shader_compiler_options options = { };
40 b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "cf test");
41 }
42
~nir_cf_test()43 nir_cf_test::~nir_cf_test()
44 {
45 ralloc_free(b.shader);
46 glsl_type_singleton_decref();
47 }
48
TEST_F(nir_cf_test,delete_break_in_loop)49 TEST_F(nir_cf_test, delete_break_in_loop)
50 {
51 /* Create IR:
52 *
53 * while (...) { break; }
54 */
55 nir_loop *loop = nir_loop_create(b.shader);
56 nir_cf_node_insert(nir_after_cf_list(&b.impl->body), &loop->cf_node);
57
58 b.cursor = nir_after_cf_list(&loop->body);
59
60 nir_jump_instr *jump = nir_jump_instr_create(b.shader, nir_jump_break);
61 nir_builder_instr_insert(&b, &jump->instr);
62
63 /* At this point, we should have:
64 *
65 * impl main {
66 * block block_0:
67 * // preds:
68 * // succs: block_1
69 * loop {
70 * block block_1:
71 * // preds: block_0
72 * break
73 * // succs: block_2
74 * }
75 * block block_2:
76 * // preds: block_1
77 * // succs: block_3
78 * block block_3:
79 * }
80 */
81 nir_block *block_0 = nir_start_block(b.impl);
82 nir_block *block_1 = nir_loop_first_block(loop);
83 nir_block *block_2 = nir_cf_node_as_block(nir_cf_node_next(&loop->cf_node));
84 nir_block *block_3 = b.impl->end_block;
85 ASSERT_EQ(nir_cf_node_block, block_0->cf_node.type);
86 ASSERT_EQ(nir_cf_node_block, block_1->cf_node.type);
87 ASSERT_EQ(nir_cf_node_block, block_2->cf_node.type);
88 ASSERT_EQ(nir_cf_node_block, block_3->cf_node.type);
89
90 /* Verify the successors and predecessors. */
91 EXPECT_EQ(block_1, block_0->successors[0]);
92 EXPECT_EQ(NULL, block_0->successors[1]);
93 EXPECT_EQ(block_2, block_1->successors[0]);
94 EXPECT_EQ(NULL, block_1->successors[1]);
95 EXPECT_EQ(block_3, block_2->successors[0]);
96 EXPECT_EQ(NULL, block_2->successors[1]);
97 EXPECT_EQ(NULL, block_3->successors[0]);
98 EXPECT_EQ(NULL, block_3->successors[1]);
99 EXPECT_EQ(0, block_0->predecessors->entries);
100 EXPECT_EQ(1, block_1->predecessors->entries);
101 EXPECT_EQ(1, block_2->predecessors->entries);
102 EXPECT_EQ(1, block_3->predecessors->entries);
103 EXPECT_TRUE(_mesa_set_search(block_1->predecessors, block_0));
104 EXPECT_TRUE(_mesa_set_search(block_2->predecessors, block_1));
105 EXPECT_TRUE(_mesa_set_search(block_3->predecessors, block_2));
106
107 nir_print_shader(b.shader, stderr);
108
109 /* Now remove the break. */
110 nir_instr_remove(&jump->instr);
111
112 nir_print_shader(b.shader, stderr);
113
114 /* At this point, we should have:
115 *
116 * impl main {
117 * block block_0:
118 * // preds:
119 * // succs: block_1
120 * loop {
121 * block block_1:
122 * // preds: block_0 block_1
123 * // succs: block_1
124 * }
125 * block block_2:
126 * // preds:
127 * // succs: block_3
128 * block block_3:
129 * }
130 *
131 * Re-verify the predecessors and successors.
132 */
133 EXPECT_EQ(block_1, block_0->successors[0]);
134 EXPECT_EQ(NULL, block_0->successors[1]);
135 EXPECT_EQ(block_1, block_1->successors[0]); /* back to itself */
136 EXPECT_EQ(NULL, block_1->successors[1]);
137 EXPECT_EQ(block_3, block_2->successors[0]);
138 EXPECT_EQ(NULL, block_2->successors[1]);
139 EXPECT_EQ(NULL, block_3->successors[0]);
140 EXPECT_EQ(NULL, block_3->successors[1]);
141 EXPECT_EQ(0, block_0->predecessors->entries);
142 EXPECT_EQ(2, block_1->predecessors->entries);
143 EXPECT_EQ(0, block_2->predecessors->entries);
144 EXPECT_EQ(1, block_3->predecessors->entries);
145 EXPECT_TRUE(_mesa_set_search(block_1->predecessors, block_0));
146 EXPECT_TRUE(_mesa_set_search(block_1->predecessors, block_1));
147 EXPECT_FALSE(_mesa_set_search(block_2->predecessors, block_1));
148 EXPECT_TRUE(_mesa_set_search(block_3->predecessors, block_2));
149
150 nir_metadata_require(b.impl, nir_metadata_dominance);
151 }
152