blob: f0f98efadbba3538ecdd1d6173f49a01072a17e7 [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
Mark Mendellfb8d2792015-03-31 22:16:59 -040017#include "arch/x86/instruction_set_features_x86.h"
Roland Levillain75be2832014-10-17 17:02:00 +010018#include "code_generator_x86.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010019#include "dead_code_elimination.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000020#include "driver/compiler_options.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010021#include "graph_checker.h"
22#include "optimizing_unit_test.h"
Roland Levillain75be2832014-10-17 17:02:00 +010023#include "pretty_printer.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010024
25#include "gtest/gtest.h"
26
27namespace art {
28
David Brazdil4833f5a2015-12-16 10:37:39 +000029class DeadCodeEliminationTest : public CommonCompilerTest {};
30
Roland Levillain72bceff2014-09-15 18:29:00 +010031static void TestCode(const uint16_t* data,
32 const std::string& expected_before,
33 const std::string& expected_after) {
34 ArenaPool pool;
35 ArenaAllocator allocator(&pool);
36 HGraph* graph = CreateCFG(&allocator, data);
37 ASSERT_NE(graph, nullptr);
38
David Brazdil4833f5a2015-12-16 10:37:39 +000039 TransformToSsa(graph);
Roland Levillain72bceff2014-09-15 18:29:00 +010040
41 StringPrettyPrinter printer_before(graph);
42 printer_before.VisitInsertionOrder();
43 std::string actual_before = printer_before.str();
44 ASSERT_EQ(actual_before, expected_before);
45
Mark Mendellfb8d2792015-03-31 22:16:59 -040046 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
47 X86InstructionSetFeatures::FromCppDefines());
48 x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), CompilerOptions());
Calin Juravle862aaef2015-04-22 13:31:47 +010049 HDeadCodeElimination(graph).Run();
Vladimir Marko655e5852015-10-12 10:38:28 +010050 SSAChecker ssa_checker(graph);
Roland Levillain75be2832014-10-17 17:02:00 +010051 ssa_checker.Run();
52 ASSERT_TRUE(ssa_checker.IsValid());
Roland Levillain72bceff2014-09-15 18:29:00 +010053
54 StringPrettyPrinter printer_after(graph);
55 printer_after.VisitInsertionOrder();
56 std::string actual_after = printer_after.str();
57 ASSERT_EQ(actual_after, expected_after);
Roland Levillain72bceff2014-09-15 18:29:00 +010058}
59
Roland Levillain72bceff2014-09-15 18:29:00 +010060/**
61 * Small three-register program.
62 *
63 * 16-bit
64 * offset
65 * ------
66 * v1 <- 1 0. const/4 v1, #+1
67 * v0 <- 0 1. const/4 v0, #+0
68 * if v1 >= 0 goto L1 2. if-gez v1, +3
69 * v0 <- v1 4. move v0, v1
70 * L1: v2 <- v0 + v1 5. add-int v2, v0, v1
71 * return-void 7. return
72 */
David Brazdil4833f5a2015-12-16 10:37:39 +000073TEST_F(DeadCodeEliminationTest, AdditionAndConditionalJump) {
Roland Levillain72bceff2014-09-15 18:29:00 +010074 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
75 Instruction::CONST_4 | 1 << 8 | 1 << 12,
76 Instruction::CONST_4 | 0 << 8 | 0 << 12,
77 Instruction::IF_GEZ | 1 << 8, 3,
78 Instruction::MOVE | 0 << 8 | 1 << 12,
79 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
80 Instruction::RETURN_VOID);
81
82 std::string expected_before =
83 "BasicBlock 0, succ: 1\n"
84 " 3: IntConstant [15, 22, 8]\n"
85 " 5: IntConstant [22, 8]\n"
86 " 19: SuspendCheck\n"
87 " 20: Goto 1\n"
88 "BasicBlock 1, pred: 0, succ: 5, 2\n"
89 " 8: GreaterThanOrEqual(3, 5) [9]\n"
90 " 9: If(8)\n"
91 "BasicBlock 2, pred: 1, succ: 3\n"
92 " 12: Goto 3\n"
Nicolas Geoffray8b20f882015-06-19 16:17:05 +010093 "BasicBlock 3, pred: 5, 2, succ: 4\n"
94 " 22: Phi(5, 3) [15]\n"
Roland Levillain72bceff2014-09-15 18:29:00 +010095 " 15: Add(22, 3)\n"
96 " 17: ReturnVoid\n"
97 "BasicBlock 4, pred: 3\n"
98 " 18: Exit\n"
99 "BasicBlock 5, pred: 1, succ: 3\n"
100 " 21: Goto 3\n";
101
Roland Levillain75be2832014-10-17 17:02:00 +0100102 // Expected difference after dead code elimination.
Roland Levillain72bceff2014-09-15 18:29:00 +0100103 diff_t expected_diff = {
104 { " 3: IntConstant [15, 22, 8]\n", " 3: IntConstant [22, 8]\n" },
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100105 { " 22: Phi(5, 3) [15]\n", " 22: Phi(5, 3)\n" },
Roland Levillain72bceff2014-09-15 18:29:00 +0100106 { " 15: Add(22, 3)\n", removed }
107 };
108 std::string expected_after = Patch(expected_before, expected_diff);
109
110 TestCode(data, expected_before, expected_after);
111}
112
113/**
114 * Three-register program with jumps leading to the creation of many
115 * blocks.
116 *
117 * The intent of this test is to ensure that all dead instructions are
118 * actually pruned at compile-time, thanks to the (backward)
119 * post-order traversal of the the dominator tree.
120 *
121 * 16-bit
122 * offset
123 * ------
124 * v0 <- 0 0. const/4 v0, #+0
125 * v1 <- 1 1. const/4 v1, #+1
126 * v2 <- v0 + v1 2. add-int v2, v0, v1
127 * goto L2 4. goto +4
128 * L1: v1 <- v0 + 3 5. add-int/lit16 v1, v0, #+3
129 * goto L3 7. goto +4
130 * L2: v0 <- v2 + 2 8. add-int/lit16 v0, v2, #+2
131 * goto L1 10. goto +(-5)
132 * L3: v2 <- v1 + 4 11. add-int/lit16 v2, v1, #+4
133 * return 13. return-void
134 */
David Brazdil4833f5a2015-12-16 10:37:39 +0000135TEST_F(DeadCodeEliminationTest, AdditionsAndInconditionalJumps) {
Roland Levillain72bceff2014-09-15 18:29:00 +0100136 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
137 Instruction::CONST_4 | 0 << 8 | 0 << 12,
138 Instruction::CONST_4 | 1 << 8 | 1 << 12,
139 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
140 Instruction::GOTO | 4 << 8,
141 Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 3,
142 Instruction::GOTO | 4 << 8,
143 Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 2,
Andreas Gampe58554b72015-10-20 21:08:52 -0700144 static_cast<uint16_t>(Instruction::GOTO | 0xFFFFFFFB << 8),
Roland Levillain72bceff2014-09-15 18:29:00 +0100145 Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 4,
146 Instruction::RETURN_VOID);
147
148 std::string expected_before =
149 "BasicBlock 0, succ: 1\n"
150 " 3: IntConstant [9]\n"
151 " 5: IntConstant [9]\n"
152 " 13: IntConstant [14]\n"
153 " 18: IntConstant [19]\n"
154 " 24: IntConstant [25]\n"
155 " 29: SuspendCheck\n"
156 " 30: Goto 1\n"
157 "BasicBlock 1, pred: 0, succ: 3\n"
158 " 9: Add(3, 5) [19]\n"
159 " 11: Goto 3\n"
160 "BasicBlock 2, pred: 3, succ: 4\n"
161 " 14: Add(19, 13) [25]\n"
162 " 16: Goto 4\n"
163 "BasicBlock 3, pred: 1, succ: 2\n"
164 " 19: Add(9, 18) [14]\n"
165 " 21: SuspendCheck\n"
166 " 22: Goto 2\n"
167 "BasicBlock 4, pred: 2, succ: 5\n"
168 " 25: Add(14, 24)\n"
169 " 27: ReturnVoid\n"
170 "BasicBlock 5, pred: 4\n"
171 " 28: Exit\n";
172
David Brazdil1c533c12015-04-24 17:04:38 +0100173 // The SuspendCheck instruction following this Add instruction
174 // inserts the latter in an environment, thus making it "used" and
175 // therefore non removable. It ensures that some other Add and
176 // IntConstant instructions cannot be removed, as they are direct
177 // or indirect inputs of the initial Add instruction.
178 std::string expected_after =
179 "BasicBlock 0, succ: 1\n"
180 " 3: IntConstant [9]\n"
181 " 5: IntConstant [9]\n"
182 " 18: IntConstant [19]\n"
183 " 29: SuspendCheck\n"
184 " 30: Goto 1\n"
185 "BasicBlock 1, pred: 0, succ: 5\n"
186 " 9: Add(3, 5) [19]\n"
187 " 19: Add(9, 18) []\n"
188 " 21: SuspendCheck\n"
189 " 27: ReturnVoid\n"
190 "BasicBlock 5, pred: 1\n"
191 " 28: Exit\n";
Roland Levillain72bceff2014-09-15 18:29:00 +0100192
193 TestCode(data, expected_before, expected_after);
194}
195
196} // namespace art