blob: 96fa5406b2b2452b9bcb32cbdb51c3165911ecea [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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "dead_code_elimination.h"
18
Mark Mendellfb8d2792015-03-31 22:16:59 -040019#include "arch/x86/instruction_set_features_x86.h"
Roland Levillain75be2832014-10-17 17:02:00 +010020#include "code_generator_x86.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000021#include "driver/compiler_options.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010022#include "graph_checker.h"
23#include "optimizing_unit_test.h"
Roland Levillain75be2832014-10-17 17:02:00 +010024#include "pretty_printer.h"
Roland Levillain72bceff2014-09-15 18:29:00 +010025
26#include "gtest/gtest.h"
27
28namespace art {
29
David Brazdil4833f5a2015-12-16 10:37:39 +000030class DeadCodeEliminationTest : public CommonCompilerTest {};
31
Roland Levillain72bceff2014-09-15 18:29:00 +010032static void TestCode(const uint16_t* data,
33 const std::string& expected_before,
34 const std::string& expected_after) {
35 ArenaPool pool;
36 ArenaAllocator allocator(&pool);
37 HGraph* graph = CreateCFG(&allocator, data);
38 ASSERT_NE(graph, nullptr);
39
Roland Levillain72bceff2014-09-15 18:29:00 +010040 StringPrettyPrinter printer_before(graph);
41 printer_before.VisitInsertionOrder();
42 std::string actual_before = printer_before.str();
43 ASSERT_EQ(actual_before, expected_before);
44
Mark Mendellfb8d2792015-03-31 22:16:59 -040045 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
46 X86InstructionSetFeatures::FromCppDefines());
47 x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), CompilerOptions());
Andreas Gampeca620d72016-11-08 08:09:33 -080048 HDeadCodeElimination(graph, nullptr /* stats */, "dead_code_elimination").Run();
David Brazdilbadd8262016-02-02 16:28:56 +000049 GraphChecker graph_checker(graph);
50 graph_checker.Run();
51 ASSERT_TRUE(graph_checker.IsValid());
Roland Levillain72bceff2014-09-15 18:29:00 +010052
53 StringPrettyPrinter printer_after(graph);
54 printer_after.VisitInsertionOrder();
55 std::string actual_after = printer_after.str();
56 ASSERT_EQ(actual_after, expected_after);
Roland Levillain72bceff2014-09-15 18:29:00 +010057}
58
Roland Levillain72bceff2014-09-15 18:29:00 +010059/**
60 * Small three-register program.
61 *
62 * 16-bit
63 * offset
64 * ------
65 * v1 <- 1 0. const/4 v1, #+1
66 * v0 <- 0 1. const/4 v0, #+0
67 * if v1 >= 0 goto L1 2. if-gez v1, +3
68 * v0 <- v1 4. move v0, v1
69 * L1: v2 <- v0 + v1 5. add-int v2, v0, v1
70 * return-void 7. return
71 */
David Brazdil4833f5a2015-12-16 10:37:39 +000072TEST_F(DeadCodeEliminationTest, AdditionAndConditionalJump) {
Roland Levillain72bceff2014-09-15 18:29:00 +010073 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
74 Instruction::CONST_4 | 1 << 8 | 1 << 12,
75 Instruction::CONST_4 | 0 << 8 | 0 << 12,
76 Instruction::IF_GEZ | 1 << 8, 3,
77 Instruction::MOVE | 0 << 8 | 1 << 12,
78 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
79 Instruction::RETURN_VOID);
80
81 std::string expected_before =
David Brazdil86ea7ee2016-02-16 09:26:07 +000082 "BasicBlock 0, succ: 1\n"
David Brazdildee58d62016-04-07 09:54:26 +000083 " 3: IntConstant [9, 8, 5]\n"
84 " 4: IntConstant [8, 5]\n"
85 " 1: SuspendCheck\n"
86 " 2: Goto 1\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000087 "BasicBlock 1, pred: 0, succ: 5, 2\n"
David Brazdildee58d62016-04-07 09:54:26 +000088 " 5: GreaterThanOrEqual(3, 4) [6]\n"
89 " 6: If(5)\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000090 "BasicBlock 2, pred: 1, succ: 3\n"
David Brazdildee58d62016-04-07 09:54:26 +000091 " 7: Goto 3\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000092 "BasicBlock 3, pred: 5, 2, succ: 4\n"
David Brazdildee58d62016-04-07 09:54:26 +000093 " 8: Phi(4, 3) [9]\n"
94 " 9: Add(8, 3)\n"
95 " 10: ReturnVoid\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000096 "BasicBlock 4, pred: 3\n"
David Brazdildee58d62016-04-07 09:54:26 +000097 " 11: Exit\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000098 "BasicBlock 5, pred: 1, succ: 3\n"
99 " 0: Goto 3\n";
Roland Levillain72bceff2014-09-15 18:29:00 +0100100
Roland Levillain75be2832014-10-17 17:02:00 +0100101 // Expected difference after dead code elimination.
Roland Levillain72bceff2014-09-15 18:29:00 +0100102 diff_t expected_diff = {
David Brazdildee58d62016-04-07 09:54:26 +0000103 { " 3: IntConstant [9, 8, 5]\n", " 3: IntConstant [8, 5]\n" },
104 { " 8: Phi(4, 3) [9]\n", " 8: Phi(4, 3)\n" },
105 { " 9: Add(8, 3)\n", removed }
Roland Levillain72bceff2014-09-15 18:29:00 +0100106 };
107 std::string expected_after = Patch(expected_before, expected_diff);
108
109 TestCode(data, expected_before, expected_after);
110}
111
112/**
113 * Three-register program with jumps leading to the creation of many
114 * blocks.
115 *
116 * The intent of this test is to ensure that all dead instructions are
117 * actually pruned at compile-time, thanks to the (backward)
118 * post-order traversal of the the dominator tree.
119 *
120 * 16-bit
121 * offset
122 * ------
123 * v0 <- 0 0. const/4 v0, #+0
124 * v1 <- 1 1. const/4 v1, #+1
125 * v2 <- v0 + v1 2. add-int v2, v0, v1
126 * goto L2 4. goto +4
127 * L1: v1 <- v0 + 3 5. add-int/lit16 v1, v0, #+3
128 * goto L3 7. goto +4
129 * L2: v0 <- v2 + 2 8. add-int/lit16 v0, v2, #+2
130 * goto L1 10. goto +(-5)
131 * L3: v2 <- v1 + 4 11. add-int/lit16 v2, v1, #+4
132 * return 13. return-void
133 */
David Brazdil4833f5a2015-12-16 10:37:39 +0000134TEST_F(DeadCodeEliminationTest, AdditionsAndInconditionalJumps) {
Roland Levillain72bceff2014-09-15 18:29:00 +0100135 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
136 Instruction::CONST_4 | 0 << 8 | 0 << 12,
137 Instruction::CONST_4 | 1 << 8 | 1 << 12,
138 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
139 Instruction::GOTO | 4 << 8,
140 Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 3,
141 Instruction::GOTO | 4 << 8,
142 Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 2,
Andreas Gampe58554b72015-10-20 21:08:52 -0700143 static_cast<uint16_t>(Instruction::GOTO | 0xFFFFFFFB << 8),
Roland Levillain72bceff2014-09-15 18:29:00 +0100144 Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 4,
145 Instruction::RETURN_VOID);
146
147 std::string expected_before =
David Brazdil86ea7ee2016-02-16 09:26:07 +0000148 "BasicBlock 0, succ: 1\n"
David Brazdildee58d62016-04-07 09:54:26 +0000149 " 2: IntConstant [4]\n"
150 " 3: IntConstant [4]\n"
151 " 6: IntConstant [7]\n"
152 " 9: IntConstant [10]\n"
153 " 12: IntConstant [13]\n"
154 " 0: SuspendCheck\n"
155 " 1: Goto 1\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000156 "BasicBlock 1, pred: 0, succ: 3\n"
David Brazdildee58d62016-04-07 09:54:26 +0000157 " 4: Add(2, 3) [7]\n"
158 " 5: Goto 3\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000159 "BasicBlock 2, pred: 3, succ: 4\n"
David Brazdildee58d62016-04-07 09:54:26 +0000160 " 10: Add(7, 9) [13]\n"
161 " 11: Goto 4\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000162 "BasicBlock 3, pred: 1, succ: 2\n"
David Brazdildee58d62016-04-07 09:54:26 +0000163 " 7: Add(4, 6) [10]\n"
164 " 8: Goto 2\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000165 "BasicBlock 4, pred: 2, succ: 5\n"
David Brazdildee58d62016-04-07 09:54:26 +0000166 " 13: Add(10, 12)\n"
167 " 14: ReturnVoid\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000168 "BasicBlock 5, pred: 4\n"
David Brazdildee58d62016-04-07 09:54:26 +0000169 " 15: Exit\n";
Roland Levillain72bceff2014-09-15 18:29:00 +0100170
David Brazdil1c533c12015-04-24 17:04:38 +0100171 std::string expected_after =
David Brazdil86ea7ee2016-02-16 09:26:07 +0000172 "BasicBlock 0, succ: 1\n"
David Brazdildee58d62016-04-07 09:54:26 +0000173 " 0: SuspendCheck\n"
174 " 1: Goto 1\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000175 "BasicBlock 1, pred: 0, succ: 5\n"
David Brazdildee58d62016-04-07 09:54:26 +0000176 " 14: ReturnVoid\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000177 "BasicBlock 5, pred: 1\n"
David Brazdildee58d62016-04-07 09:54:26 +0000178 " 15: Exit\n";
Roland Levillain72bceff2014-09-15 18:29:00 +0100179
180 TestCode(data, expected_before, expected_after);
181}
182
183} // namespace art