blob: 245bcb21d55b5e5b4f342e6ebac7ee1d7384505e [file] [log] [blame]
Roland Levillain72bceff2014-09-15 18:29:00 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "dead_code_elimination.h"
18#include "pretty_printer.h"
19#include "graph_checker.h"
20#include "optimizing_unit_test.h"
21
22#include "gtest/gtest.h"
23
24namespace art {
25
26static void TestCode(const uint16_t* data,
27 const std::string& expected_before,
28 const std::string& expected_after) {
29 ArenaPool pool;
30 ArenaAllocator allocator(&pool);
31 HGraph* graph = CreateCFG(&allocator, data);
32 ASSERT_NE(graph, nullptr);
33
34 graph->BuildDominatorTree();
35 graph->TransformToSSA();
36
37 StringPrettyPrinter printer_before(graph);
38 printer_before.VisitInsertionOrder();
39 std::string actual_before = printer_before.str();
40 ASSERT_EQ(actual_before, expected_before);
41
42 DeadCodeElimination(graph).Run();
43
44 StringPrettyPrinter printer_after(graph);
45 printer_after.VisitInsertionOrder();
46 std::string actual_after = printer_after.str();
47 ASSERT_EQ(actual_after, expected_after);
48
49 SSAChecker ssa_checker(&allocator, graph);
50 ssa_checker.VisitInsertionOrder();
51 ASSERT_TRUE(ssa_checker.IsValid());
52}
53
54
55/**
56 * Small three-register program.
57 *
58 * 16-bit
59 * offset
60 * ------
61 * v1 <- 1 0. const/4 v1, #+1
62 * v0 <- 0 1. const/4 v0, #+0
63 * if v1 >= 0 goto L1 2. if-gez v1, +3
64 * v0 <- v1 4. move v0, v1
65 * L1: v2 <- v0 + v1 5. add-int v2, v0, v1
66 * return-void 7. return
67 */
68TEST(DeadCodeElimination, AdditionAndConditionalJump) {
69 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
70 Instruction::CONST_4 | 1 << 8 | 1 << 12,
71 Instruction::CONST_4 | 0 << 8 | 0 << 12,
72 Instruction::IF_GEZ | 1 << 8, 3,
73 Instruction::MOVE | 0 << 8 | 1 << 12,
74 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
75 Instruction::RETURN_VOID);
76
77 std::string expected_before =
78 "BasicBlock 0, succ: 1\n"
79 " 3: IntConstant [15, 22, 8]\n"
80 " 5: IntConstant [22, 8]\n"
81 " 19: SuspendCheck\n"
82 " 20: Goto 1\n"
83 "BasicBlock 1, pred: 0, succ: 5, 2\n"
84 " 8: GreaterThanOrEqual(3, 5) [9]\n"
85 " 9: If(8)\n"
86 "BasicBlock 2, pred: 1, succ: 3\n"
87 " 12: Goto 3\n"
88 "BasicBlock 3, pred: 2, 5, succ: 4\n"
89 " 22: Phi(3, 5) [15]\n"
90 " 15: Add(22, 3)\n"
91 " 17: ReturnVoid\n"
92 "BasicBlock 4, pred: 3\n"
93 " 18: Exit\n"
94 "BasicBlock 5, pred: 1, succ: 3\n"
95 " 21: Goto 3\n";
96
97 diff_t expected_diff = {
98 { " 3: IntConstant [15, 22, 8]\n", " 3: IntConstant [22, 8]\n" },
99 { " 22: Phi(3, 5) [15]\n", " 22: Phi(3, 5)\n" },
100 { " 15: Add(22, 3)\n", removed }
101 };
102 std::string expected_after = Patch(expected_before, expected_diff);
103
104 TestCode(data, expected_before, expected_after);
105}
106
107/**
108 * Three-register program with jumps leading to the creation of many
109 * blocks.
110 *
111 * The intent of this test is to ensure that all dead instructions are
112 * actually pruned at compile-time, thanks to the (backward)
113 * post-order traversal of the the dominator tree.
114 *
115 * 16-bit
116 * offset
117 * ------
118 * v0 <- 0 0. const/4 v0, #+0
119 * v1 <- 1 1. const/4 v1, #+1
120 * v2 <- v0 + v1 2. add-int v2, v0, v1
121 * goto L2 4. goto +4
122 * L1: v1 <- v0 + 3 5. add-int/lit16 v1, v0, #+3
123 * goto L3 7. goto +4
124 * L2: v0 <- v2 + 2 8. add-int/lit16 v0, v2, #+2
125 * goto L1 10. goto +(-5)
126 * L3: v2 <- v1 + 4 11. add-int/lit16 v2, v1, #+4
127 * return 13. return-void
128 */
129TEST(DeadCodeElimination, AdditionsAndInconditionalJumps) {
130 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
131 Instruction::CONST_4 | 0 << 8 | 0 << 12,
132 Instruction::CONST_4 | 1 << 8 | 1 << 12,
133 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
134 Instruction::GOTO | 4 << 8,
135 Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 3,
136 Instruction::GOTO | 4 << 8,
137 Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 2,
138 static_cast<uint16_t>(Instruction::GOTO | -5 << 8),
139 Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 4,
140 Instruction::RETURN_VOID);
141
142 std::string expected_before =
143 "BasicBlock 0, succ: 1\n"
144 " 3: IntConstant [9]\n"
145 " 5: IntConstant [9]\n"
146 " 13: IntConstant [14]\n"
147 " 18: IntConstant [19]\n"
148 " 24: IntConstant [25]\n"
149 " 29: SuspendCheck\n"
150 " 30: Goto 1\n"
151 "BasicBlock 1, pred: 0, succ: 3\n"
152 " 9: Add(3, 5) [19]\n"
153 " 11: Goto 3\n"
154 "BasicBlock 2, pred: 3, succ: 4\n"
155 " 14: Add(19, 13) [25]\n"
156 " 16: Goto 4\n"
157 "BasicBlock 3, pred: 1, succ: 2\n"
158 " 19: Add(9, 18) [14]\n"
159 " 21: SuspendCheck\n"
160 " 22: Goto 2\n"
161 "BasicBlock 4, pred: 2, succ: 5\n"
162 " 25: Add(14, 24)\n"
163 " 27: ReturnVoid\n"
164 "BasicBlock 5, pred: 4\n"
165 " 28: Exit\n";
166
167 // Expected difference after constant propagation.
168 diff_t expected_diff = {
169 { " 13: IntConstant [14]\n", removed },
170 { " 24: IntConstant [25]\n", removed },
171 { " 14: Add(19, 13) [25]\n", removed },
172 // The SuspendCheck instruction following this Add instruction
173 // inserts the latter in an environment, thus making it "used" and
174 // therefore non removable. It ensues that some other Add and
175 // IntConstant instructions cannot be removed, as they are direct
176 // or indirect inputs of the initial Add instruction.
177 { " 19: Add(9, 18) [14]\n", " 19: Add(9, 18) []\n" },
178 { " 25: Add(14, 24)\n", removed },
179 };
180 std::string expected_after = Patch(expected_before, expected_diff);
181
182 TestCode(data, expected_before, expected_after);
183}
184
185} // namespace art