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 static const nir_shader_compiler_options options = { };
38 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, &options);
39 }
40
~nir_cf_test()41 nir_cf_test::~nir_cf_test()
42 {
43 ralloc_free(b.shader);
44 }
45
TEST_F(nir_cf_test,delete_break_in_loop)46 TEST_F(nir_cf_test, delete_break_in_loop)
47 {
48 /* Create IR:
49 *
50 * while (...) { break; }
51 */
52 nir_loop *loop = nir_loop_create(b.shader);
53 nir_cf_node_insert(nir_after_cf_list(&b.impl->body), &loop->cf_node);
54
55 b.cursor = nir_after_cf_list(&loop->body);
56
57 nir_jump_instr *jump = nir_jump_instr_create(b.shader, nir_jump_break);
58 nir_builder_instr_insert(&b, &jump->instr);
59
60 /* At this point, we should have:
61 *
62 * impl main {
63 * block block_0:
64 * // preds:
65 * // succs: block_1
66 * loop {
67 * block block_1:
68 * // preds: block_0
69 * break
70 * // succs: block_2
71 * }
72 * block block_2:
73 * // preds: block_1
74 * // succs: block_3
75 * block block_3:
76 * }
77 */
78 nir_block *block_0 = nir_start_block(b.impl);
79 nir_block *block_1 = nir_loop_first_block(loop);
80 nir_block *block_2 = nir_cf_node_as_block(nir_cf_node_next(&loop->cf_node));
81 nir_block *block_3 = b.impl->end_block;
82 ASSERT_EQ(nir_cf_node_block, block_0->cf_node.type);
83 ASSERT_EQ(nir_cf_node_block, block_1->cf_node.type);
84 ASSERT_EQ(nir_cf_node_block, block_2->cf_node.type);
85 ASSERT_EQ(nir_cf_node_block, block_3->cf_node.type);
86
87 /* Verify the successors and predecessors. */
88 EXPECT_EQ(block_1, block_0->successors[0]);
89 EXPECT_EQ(NULL, block_0->successors[1]);
90 EXPECT_EQ(block_2, block_1->successors[0]);
91 EXPECT_EQ(NULL, block_1->successors[1]);
92 EXPECT_EQ(block_3, block_2->successors[0]);
93 EXPECT_EQ(NULL, block_2->successors[1]);
94 EXPECT_EQ(NULL, block_3->successors[0]);
95 EXPECT_EQ(NULL, block_3->successors[1]);
96 EXPECT_EQ(0, block_0->predecessors->entries);
97 EXPECT_EQ(1, block_1->predecessors->entries);
98 EXPECT_EQ(1, block_2->predecessors->entries);
99 EXPECT_EQ(1, block_3->predecessors->entries);
100 EXPECT_TRUE(_mesa_set_search(block_1->predecessors, block_0));
101 EXPECT_TRUE(_mesa_set_search(block_2->predecessors, block_1));
102 EXPECT_TRUE(_mesa_set_search(block_3->predecessors, block_2));
103
104 nir_print_shader(b.shader, stderr);
105
106 /* Now remove the break. */
107 nir_instr_remove(&jump->instr);
108
109 nir_print_shader(b.shader, stderr);
110
111 /* At this point, we should have:
112 *
113 * impl main {
114 * block block_0:
115 * // preds:
116 * // succs: block_1
117 * loop {
118 * block block_1:
119 * // preds: block_0 block_1
120 * // succs: block_1
121 * }
122 * block block_2:
123 * // preds:
124 * // succs: block_3
125 * block block_3:
126 * }
127 *
128 * Re-verify the predecessors and successors.
129 */
130 EXPECT_EQ(block_1, block_0->successors[0]);
131 EXPECT_EQ(NULL, block_0->successors[1]);
132 EXPECT_EQ(block_1, block_1->successors[0]); /* back to itself */
133 EXPECT_EQ(NULL, block_1->successors[1]);
134 EXPECT_EQ(block_3, block_2->successors[0]);
135 EXPECT_EQ(NULL, block_2->successors[1]);
136 EXPECT_EQ(NULL, block_3->successors[0]);
137 EXPECT_EQ(NULL, block_3->successors[1]);
138 EXPECT_EQ(0, block_0->predecessors->entries);
139 EXPECT_EQ(2, block_1->predecessors->entries);
140 EXPECT_EQ(0, block_2->predecessors->entries);
141 EXPECT_EQ(1, block_3->predecessors->entries);
142 EXPECT_TRUE(_mesa_set_search(block_1->predecessors, block_0));
143 EXPECT_TRUE(_mesa_set_search(block_1->predecessors, block_1));
144 EXPECT_FALSE(_mesa_set_search(block_2->predecessors, block_1));
145 EXPECT_TRUE(_mesa_set_search(block_3->predecessors, block_2));
146
147 nir_metadata_require(b.impl, nir_metadata_dominance);
148 }
149