blob: 277453545a66127963be2c373419d22b904cd1a8 [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
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
Vladimir Markoca6fff82017-10-03 14:49:14 +010028class DeadCodeEliminationTest : public OptimizingUnitTest {
29 protected:
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080030 void TestCode(const std::vector<uint16_t>& data,
Vladimir Markoca6fff82017-10-03 14:49:14 +010031 const std::string& expected_before,
32 const std::string& expected_after);
33};
David Brazdil4833f5a2015-12-16 10:37:39 +000034
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080035void DeadCodeEliminationTest::TestCode(const std::vector<uint16_t>& data,
Vladimir Markoca6fff82017-10-03 14:49:14 +010036 const std::string& expected_before,
37 const std::string& expected_after) {
38 HGraph* graph = CreateCFG(data);
Roland Levillain72bceff2014-09-15 18:29:00 +010039 ASSERT_NE(graph, nullptr);
40
Roland Levillain72bceff2014-09-15 18:29:00 +010041 StringPrettyPrinter printer_before(graph);
42 printer_before.VisitInsertionOrder();
43 std::string actual_before = printer_before.str();
44 ASSERT_EQ(actual_before, expected_before);
45
Andreas Gampeca620d72016-11-08 08:09:33 -080046 HDeadCodeElimination(graph, nullptr /* stats */, "dead_code_elimination").Run();
David Brazdilbadd8262016-02-02 16:28:56 +000047 GraphChecker graph_checker(graph);
48 graph_checker.Run();
49 ASSERT_TRUE(graph_checker.IsValid());
Roland Levillain72bceff2014-09-15 18:29:00 +010050
51 StringPrettyPrinter printer_after(graph);
52 printer_after.VisitInsertionOrder();
53 std::string actual_after = printer_after.str();
54 ASSERT_EQ(actual_after, expected_after);
Roland Levillain72bceff2014-09-15 18:29:00 +010055}
56
Roland Levillain72bceff2014-09-15 18:29:00 +010057/**
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 */
David Brazdil4833f5a2015-12-16 10:37:39 +000070TEST_F(DeadCodeEliminationTest, AdditionAndConditionalJump) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080071 const std::vector<uint16_t> data = THREE_REGISTERS_CODE_ITEM(
Roland Levillain72bceff2014-09-15 18:29:00 +010072 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 =
David Brazdil86ea7ee2016-02-16 09:26:07 +000080 "BasicBlock 0, succ: 1\n"
David Brazdildee58d62016-04-07 09:54:26 +000081 " 3: IntConstant [9, 8, 5]\n"
82 " 4: IntConstant [8, 5]\n"
83 " 1: SuspendCheck\n"
84 " 2: Goto 1\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000085 "BasicBlock 1, pred: 0, succ: 5, 2\n"
David Brazdildee58d62016-04-07 09:54:26 +000086 " 5: GreaterThanOrEqual(3, 4) [6]\n"
87 " 6: If(5)\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000088 "BasicBlock 2, pred: 1, succ: 3\n"
David Brazdildee58d62016-04-07 09:54:26 +000089 " 7: Goto 3\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000090 "BasicBlock 3, pred: 5, 2, succ: 4\n"
David Brazdildee58d62016-04-07 09:54:26 +000091 " 8: Phi(4, 3) [9]\n"
92 " 9: Add(8, 3)\n"
93 " 10: ReturnVoid\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000094 "BasicBlock 4, pred: 3\n"
David Brazdildee58d62016-04-07 09:54:26 +000095 " 11: Exit\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +000096 "BasicBlock 5, pred: 1, succ: 3\n"
97 " 0: Goto 3\n";
Roland Levillain72bceff2014-09-15 18:29:00 +010098
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 = {
David Brazdildee58d62016-04-07 09:54:26 +0000101 { " 3: IntConstant [9, 8, 5]\n", " 3: IntConstant [8, 5]\n" },
102 { " 8: Phi(4, 3) [9]\n", " 8: Phi(4, 3)\n" },
103 { " 9: Add(8, 3)\n", removed }
Roland Levillain72bceff2014-09-15 18:29:00 +0100104 };
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 */
David Brazdil4833f5a2015-12-16 10:37:39 +0000132TEST_F(DeadCodeEliminationTest, AdditionsAndInconditionalJumps) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800133 const std::vector<uint16_t> data = THREE_REGISTERS_CODE_ITEM(
Roland Levillain72bceff2014-09-15 18:29:00 +0100134 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,
Andreas Gampe58554b72015-10-20 21:08:52 -0700141 static_cast<uint16_t>(Instruction::GOTO | 0xFFFFFFFB << 8),
Roland Levillain72bceff2014-09-15 18:29:00 +0100142 Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 4,
143 Instruction::RETURN_VOID);
144
145 std::string expected_before =
David Brazdil86ea7ee2016-02-16 09:26:07 +0000146 "BasicBlock 0, succ: 1\n"
David Brazdildee58d62016-04-07 09:54:26 +0000147 " 2: IntConstant [4]\n"
148 " 3: IntConstant [4]\n"
149 " 6: IntConstant [7]\n"
150 " 9: IntConstant [10]\n"
151 " 12: IntConstant [13]\n"
152 " 0: SuspendCheck\n"
153 " 1: Goto 1\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000154 "BasicBlock 1, pred: 0, succ: 3\n"
David Brazdildee58d62016-04-07 09:54:26 +0000155 " 4: Add(2, 3) [7]\n"
156 " 5: Goto 3\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000157 "BasicBlock 2, pred: 3, succ: 4\n"
David Brazdildee58d62016-04-07 09:54:26 +0000158 " 10: Add(7, 9) [13]\n"
159 " 11: Goto 4\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000160 "BasicBlock 3, pred: 1, succ: 2\n"
David Brazdildee58d62016-04-07 09:54:26 +0000161 " 7: Add(4, 6) [10]\n"
162 " 8: Goto 2\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000163 "BasicBlock 4, pred: 2, succ: 5\n"
David Brazdildee58d62016-04-07 09:54:26 +0000164 " 13: Add(10, 12)\n"
165 " 14: ReturnVoid\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000166 "BasicBlock 5, pred: 4\n"
David Brazdildee58d62016-04-07 09:54:26 +0000167 " 15: Exit\n";
Roland Levillain72bceff2014-09-15 18:29:00 +0100168
David Brazdil1c533c12015-04-24 17:04:38 +0100169 std::string expected_after =
David Brazdil86ea7ee2016-02-16 09:26:07 +0000170 "BasicBlock 0, succ: 1\n"
David Brazdildee58d62016-04-07 09:54:26 +0000171 " 0: SuspendCheck\n"
172 " 1: Goto 1\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000173 "BasicBlock 1, pred: 0, succ: 5\n"
David Brazdildee58d62016-04-07 09:54:26 +0000174 " 14: ReturnVoid\n"
David Brazdil86ea7ee2016-02-16 09:26:07 +0000175 "BasicBlock 5, pred: 1\n"
David Brazdildee58d62016-04-07 09:54:26 +0000176 " 15: Exit\n";
Roland Levillain72bceff2014-09-15 18:29:00 +0100177
178 TestCode(data, expected_before, expected_after);
179}
180
181} // namespace art