blob: 0c6807482abda36be554bf950ecb05ff603d3b20 [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
Roland Levillain75be2832014-10-17 17:02:00 +010017#include "code_generator_x86.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010018#include "dead_code_elimination.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010019#include "graph_checker.h"
20#include "optimizing_unit_test.h"
Roland Levillain75be2832014-10-17 17:02:00 +010021#include "pretty_printer.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010022
23#include "gtest/gtest.h"
24
25namespace art {
26
27static void TestCode(const uint16_t* data,
28 const std::string& expected_before,
29 const std::string& expected_after) {
30 ArenaPool pool;
31 ArenaAllocator allocator(&pool);
32 HGraph* graph = CreateCFG(&allocator, data);
33 ASSERT_NE(graph, nullptr);
34
35 graph->BuildDominatorTree();
36 graph->TransformToSSA();
37
38 StringPrettyPrinter printer_before(graph);
39 printer_before.VisitInsertionOrder();
40 std::string actual_before = printer_before.str();
41 ASSERT_EQ(actual_before, expected_before);
42
Roland Levillain75be2832014-10-17 17:02:00 +010043 x86::CodeGeneratorX86 codegen(graph);
44 HGraphVisualizer visualizer(nullptr, graph, codegen, "");
45 HDeadCodeElimination(graph, visualizer).Run();
46 SSAChecker ssa_checker(&allocator, graph);
47 ssa_checker.Run();
48 ASSERT_TRUE(ssa_checker.IsValid());
Roland Levillain72bceff2014-09-15 18:29:00 +010049
50 StringPrettyPrinter printer_after(graph);
51 printer_after.VisitInsertionOrder();
52 std::string actual_after = printer_after.str();
53 ASSERT_EQ(actual_after, expected_after);
Roland Levillain72bceff2014-09-15 18:29:00 +010054}
55
56
57/**
58 * Small three-register program.
59 *
60 * 16-bit
61 * offset
62 * ------
63 * v1 <- 1 0. const/4 v1, #+1
64 * v0 <- 0 1. const/4 v0, #+0
65 * if v1 >= 0 goto L1 2. if-gez v1, +3
66 * v0 <- v1 4. move v0, v1
67 * L1: v2 <- v0 + v1 5. add-int v2, v0, v1
68 * return-void 7. return
69 */
70TEST(DeadCodeElimination, AdditionAndConditionalJump) {
71 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
72 Instruction::CONST_4 | 1 << 8 | 1 << 12,
73 Instruction::CONST_4 | 0 << 8 | 0 << 12,
74 Instruction::IF_GEZ | 1 << 8, 3,
75 Instruction::MOVE | 0 << 8 | 1 << 12,
76 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
77 Instruction::RETURN_VOID);
78
79 std::string expected_before =
80 "BasicBlock 0, succ: 1\n"
81 " 3: IntConstant [15, 22, 8]\n"
82 " 5: IntConstant [22, 8]\n"
83 " 19: SuspendCheck\n"
84 " 20: Goto 1\n"
85 "BasicBlock 1, pred: 0, succ: 5, 2\n"
86 " 8: GreaterThanOrEqual(3, 5) [9]\n"
87 " 9: If(8)\n"
88 "BasicBlock 2, pred: 1, succ: 3\n"
89 " 12: Goto 3\n"
90 "BasicBlock 3, pred: 2, 5, succ: 4\n"
91 " 22: Phi(3, 5) [15]\n"
92 " 15: Add(22, 3)\n"
93 " 17: ReturnVoid\n"
94 "BasicBlock 4, pred: 3\n"
95 " 18: Exit\n"
96 "BasicBlock 5, pred: 1, succ: 3\n"
97 " 21: Goto 3\n";
98
Roland Levillain75be2832014-10-17 17:02:00 +010099 // Expected difference after dead code elimination.
Roland Levillain72bceff2014-09-15 18:29:00 +0100100 diff_t expected_diff = {
101 { " 3: IntConstant [15, 22, 8]\n", " 3: IntConstant [22, 8]\n" },
102 { " 22: Phi(3, 5) [15]\n", " 22: Phi(3, 5)\n" },
103 { " 15: Add(22, 3)\n", removed }
104 };
105 std::string expected_after = Patch(expected_before, expected_diff);
106
107 TestCode(data, expected_before, expected_after);
108}
109
110/**
111 * Three-register program with jumps leading to the creation of many
112 * blocks.
113 *
114 * The intent of this test is to ensure that all dead instructions are
115 * actually pruned at compile-time, thanks to the (backward)
116 * post-order traversal of the the dominator tree.
117 *
118 * 16-bit
119 * offset
120 * ------
121 * v0 <- 0 0. const/4 v0, #+0
122 * v1 <- 1 1. const/4 v1, #+1
123 * v2 <- v0 + v1 2. add-int v2, v0, v1
124 * goto L2 4. goto +4
125 * L1: v1 <- v0 + 3 5. add-int/lit16 v1, v0, #+3
126 * goto L3 7. goto +4
127 * L2: v0 <- v2 + 2 8. add-int/lit16 v0, v2, #+2
128 * goto L1 10. goto +(-5)
129 * L3: v2 <- v1 + 4 11. add-int/lit16 v2, v1, #+4
130 * return 13. return-void
131 */
132TEST(DeadCodeElimination, AdditionsAndInconditionalJumps) {
133 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
134 Instruction::CONST_4 | 0 << 8 | 0 << 12,
135 Instruction::CONST_4 | 1 << 8 | 1 << 12,
136 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
137 Instruction::GOTO | 4 << 8,
138 Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 3,
139 Instruction::GOTO | 4 << 8,
140 Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 2,
141 static_cast<uint16_t>(Instruction::GOTO | -5 << 8),
142 Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 4,
143 Instruction::RETURN_VOID);
144
145 std::string expected_before =
146 "BasicBlock 0, succ: 1\n"
147 " 3: IntConstant [9]\n"
148 " 5: IntConstant [9]\n"
149 " 13: IntConstant [14]\n"
150 " 18: IntConstant [19]\n"
151 " 24: IntConstant [25]\n"
152 " 29: SuspendCheck\n"
153 " 30: Goto 1\n"
154 "BasicBlock 1, pred: 0, succ: 3\n"
155 " 9: Add(3, 5) [19]\n"
156 " 11: Goto 3\n"
157 "BasicBlock 2, pred: 3, succ: 4\n"
158 " 14: Add(19, 13) [25]\n"
159 " 16: Goto 4\n"
160 "BasicBlock 3, pred: 1, succ: 2\n"
161 " 19: Add(9, 18) [14]\n"
162 " 21: SuspendCheck\n"
163 " 22: Goto 2\n"
164 "BasicBlock 4, pred: 2, succ: 5\n"
165 " 25: Add(14, 24)\n"
166 " 27: ReturnVoid\n"
167 "BasicBlock 5, pred: 4\n"
168 " 28: Exit\n";
169
Roland Levillain75be2832014-10-17 17:02:00 +0100170 // Expected difference after dead code elimination.
Roland Levillain72bceff2014-09-15 18:29:00 +0100171 diff_t expected_diff = {
172 { " 13: IntConstant [14]\n", removed },
173 { " 24: IntConstant [25]\n", removed },
174 { " 14: Add(19, 13) [25]\n", removed },
175 // The SuspendCheck instruction following this Add instruction
176 // inserts the latter in an environment, thus making it "used" and
177 // therefore non removable. It ensues that some other Add and
178 // IntConstant instructions cannot be removed, as they are direct
179 // or indirect inputs of the initial Add instruction.
180 { " 19: Add(9, 18) [14]\n", " 19: Add(9, 18) []\n" },
181 { " 25: Add(14, 24)\n", removed },
182 };
183 std::string expected_after = Patch(expected_before, expected_diff);
184
185 TestCode(data, expected_before, expected_after);
186}
187
188} // namespace art