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