1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "dead_code_elimination.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
20*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
21*795d594fSAndroid Build Coastguard Worker #include "graph_checker.h"
22*795d594fSAndroid Build Coastguard Worker #include "optimizing_unit_test.h"
23*795d594fSAndroid Build Coastguard Worker #include "pretty_printer.h"
24*795d594fSAndroid Build Coastguard Worker
25*795d594fSAndroid Build Coastguard Worker #include "gtest/gtest.h"
26*795d594fSAndroid Build Coastguard Worker
27*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard Worker class DeadCodeEliminationTest : public CommonCompilerTest, public OptimizingUnitTestHelper {
30*795d594fSAndroid Build Coastguard Worker protected:
31*795d594fSAndroid Build Coastguard Worker void TestCode(const std::vector<uint16_t>& data,
32*795d594fSAndroid Build Coastguard Worker const std::string& expected_before,
33*795d594fSAndroid Build Coastguard Worker const std::string& expected_after);
34*795d594fSAndroid Build Coastguard Worker };
35*795d594fSAndroid Build Coastguard Worker
TestCode(const std::vector<uint16_t> & data,const std::string & expected_before,const std::string & expected_after)36*795d594fSAndroid Build Coastguard Worker void DeadCodeEliminationTest::TestCode(const std::vector<uint16_t>& data,
37*795d594fSAndroid Build Coastguard Worker const std::string& expected_before,
38*795d594fSAndroid Build Coastguard Worker const std::string& expected_after) {
39*795d594fSAndroid Build Coastguard Worker HGraph* graph = CreateCFG(data);
40*795d594fSAndroid Build Coastguard Worker ASSERT_NE(graph, nullptr);
41*795d594fSAndroid Build Coastguard Worker
42*795d594fSAndroid Build Coastguard Worker StringPrettyPrinter printer_before(graph);
43*795d594fSAndroid Build Coastguard Worker printer_before.VisitInsertionOrder();
44*795d594fSAndroid Build Coastguard Worker std::string actual_before = printer_before.str();
45*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(actual_before, expected_before);
46*795d594fSAndroid Build Coastguard Worker
47*795d594fSAndroid Build Coastguard Worker HDeadCodeElimination(graph, /* stats= */ nullptr, "dead_code_elimination").Run();
48*795d594fSAndroid Build Coastguard Worker GraphChecker graph_checker(graph);
49*795d594fSAndroid Build Coastguard Worker graph_checker.Run();
50*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(graph_checker.IsValid());
51*795d594fSAndroid Build Coastguard Worker
52*795d594fSAndroid Build Coastguard Worker StringPrettyPrinter printer_after(graph);
53*795d594fSAndroid Build Coastguard Worker printer_after.VisitInsertionOrder();
54*795d594fSAndroid Build Coastguard Worker std::string actual_after = printer_after.str();
55*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(actual_after, expected_after);
56*795d594fSAndroid Build Coastguard Worker }
57*795d594fSAndroid Build Coastguard Worker
58*795d594fSAndroid Build Coastguard Worker /**
59*795d594fSAndroid Build Coastguard Worker * Small three-register program.
60*795d594fSAndroid Build Coastguard Worker *
61*795d594fSAndroid Build Coastguard Worker * 16-bit
62*795d594fSAndroid Build Coastguard Worker * offset
63*795d594fSAndroid Build Coastguard Worker * ------
64*795d594fSAndroid Build Coastguard Worker * v1 <- 1 0. const/4 v1, #+1
65*795d594fSAndroid Build Coastguard Worker * v0 <- 0 1. const/4 v0, #+0
66*795d594fSAndroid Build Coastguard Worker * if v1 >= 0 goto L1 2. if-gez v1, +3
67*795d594fSAndroid Build Coastguard Worker * v0 <- v1 4. move v0, v1
68*795d594fSAndroid Build Coastguard Worker * L1: v2 <- v0 + v1 5. add-int v2, v0, v1
69*795d594fSAndroid Build Coastguard Worker * return-void 7. return
70*795d594fSAndroid Build Coastguard Worker */
TEST_F(DeadCodeEliminationTest,AdditionAndConditionalJump)71*795d594fSAndroid Build Coastguard Worker TEST_F(DeadCodeEliminationTest, AdditionAndConditionalJump) {
72*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = THREE_REGISTERS_CODE_ITEM(
73*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 1 << 8 | 1 << 12,
74*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 << 8 | 0 << 12,
75*795d594fSAndroid Build Coastguard Worker Instruction::IF_GEZ | 1 << 8, 3,
76*795d594fSAndroid Build Coastguard Worker Instruction::MOVE | 0 << 8 | 1 << 12,
77*795d594fSAndroid Build Coastguard Worker Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
78*795d594fSAndroid Build Coastguard Worker Instruction::RETURN_VOID);
79*795d594fSAndroid Build Coastguard Worker
80*795d594fSAndroid Build Coastguard Worker std::string expected_before =
81*795d594fSAndroid Build Coastguard Worker "BasicBlock 0, succ: 1\n"
82*795d594fSAndroid Build Coastguard Worker " 3: IntConstant [9, 8, 5]\n"
83*795d594fSAndroid Build Coastguard Worker " 4: IntConstant [8, 5]\n"
84*795d594fSAndroid Build Coastguard Worker " 1: SuspendCheck\n"
85*795d594fSAndroid Build Coastguard Worker " 2: Goto 1\n"
86*795d594fSAndroid Build Coastguard Worker "BasicBlock 1, pred: 0, succ: 5, 2\n"
87*795d594fSAndroid Build Coastguard Worker " 5: GreaterThanOrEqual(3, 4) [6]\n"
88*795d594fSAndroid Build Coastguard Worker " 6: If(5)\n"
89*795d594fSAndroid Build Coastguard Worker "BasicBlock 2, pred: 1, succ: 3\n"
90*795d594fSAndroid Build Coastguard Worker " 7: Goto 3\n"
91*795d594fSAndroid Build Coastguard Worker "BasicBlock 3, pred: 5, 2, succ: 4\n"
92*795d594fSAndroid Build Coastguard Worker " 8: Phi(4, 3) [9]\n"
93*795d594fSAndroid Build Coastguard Worker " 9: Add(8, 3)\n"
94*795d594fSAndroid Build Coastguard Worker " 10: ReturnVoid\n"
95*795d594fSAndroid Build Coastguard Worker "BasicBlock 4, pred: 3\n"
96*795d594fSAndroid Build Coastguard Worker " 11: Exit\n"
97*795d594fSAndroid Build Coastguard Worker "BasicBlock 5, pred: 1, succ: 3\n"
98*795d594fSAndroid Build Coastguard Worker " 0: Goto 3\n";
99*795d594fSAndroid Build Coastguard Worker
100*795d594fSAndroid Build Coastguard Worker // Expected difference after dead code elimination.
101*795d594fSAndroid Build Coastguard Worker diff_t expected_diff = {
102*795d594fSAndroid Build Coastguard Worker { " 3: IntConstant [9, 8, 5]\n", " 3: IntConstant [5]\n" },
103*795d594fSAndroid Build Coastguard Worker { " 4: IntConstant [8, 5]\n", " 4: IntConstant [5]\n" },
104*795d594fSAndroid Build Coastguard Worker { " 8: Phi(4, 3) [9]\n", removed },
105*795d594fSAndroid Build Coastguard Worker { " 9: Add(8, 3)\n", removed }
106*795d594fSAndroid Build Coastguard Worker };
107*795d594fSAndroid Build Coastguard Worker std::string expected_after = Patch(expected_before, expected_diff);
108*795d594fSAndroid Build Coastguard Worker
109*795d594fSAndroid Build Coastguard Worker TestCode(data, expected_before, expected_after);
110*795d594fSAndroid Build Coastguard Worker }
111*795d594fSAndroid Build Coastguard Worker
112*795d594fSAndroid Build Coastguard Worker /**
113*795d594fSAndroid Build Coastguard Worker * Three-register program with jumps leading to the creation of many
114*795d594fSAndroid Build Coastguard Worker * blocks.
115*795d594fSAndroid Build Coastguard Worker *
116*795d594fSAndroid Build Coastguard Worker * The intent of this test is to ensure that all dead instructions are
117*795d594fSAndroid Build Coastguard Worker * actually pruned at compile-time, thanks to the (backward)
118*795d594fSAndroid Build Coastguard Worker * post-order traversal of the dominator tree.
119*795d594fSAndroid Build Coastguard Worker *
120*795d594fSAndroid Build Coastguard Worker * 16-bit
121*795d594fSAndroid Build Coastguard Worker * offset
122*795d594fSAndroid Build Coastguard Worker * ------
123*795d594fSAndroid Build Coastguard Worker * v0 <- 0 0. const/4 v0, #+0
124*795d594fSAndroid Build Coastguard Worker * v1 <- 1 1. const/4 v1, #+1
125*795d594fSAndroid Build Coastguard Worker * v2 <- v0 + v1 2. add-int v2, v0, v1
126*795d594fSAndroid Build Coastguard Worker * goto L2 4. goto +4
127*795d594fSAndroid Build Coastguard Worker * L1: v1 <- v0 + 3 5. add-int/lit16 v1, v0, #+3
128*795d594fSAndroid Build Coastguard Worker * goto L3 7. goto +4
129*795d594fSAndroid Build Coastguard Worker * L2: v0 <- v2 + 2 8. add-int/lit16 v0, v2, #+2
130*795d594fSAndroid Build Coastguard Worker * goto L1 10. goto +(-5)
131*795d594fSAndroid Build Coastguard Worker * L3: v2 <- v1 + 4 11. add-int/lit16 v2, v1, #+4
132*795d594fSAndroid Build Coastguard Worker * return 13. return-void
133*795d594fSAndroid Build Coastguard Worker */
TEST_F(DeadCodeEliminationTest,AdditionsAndInconditionalJumps)134*795d594fSAndroid Build Coastguard Worker TEST_F(DeadCodeEliminationTest, AdditionsAndInconditionalJumps) {
135*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = THREE_REGISTERS_CODE_ITEM(
136*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 << 8 | 0 << 12,
137*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 1 << 8 | 1 << 12,
138*795d594fSAndroid Build Coastguard Worker Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
139*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 4 << 8,
140*795d594fSAndroid Build Coastguard Worker Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 3,
141*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 4 << 8,
142*795d594fSAndroid Build Coastguard Worker Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 2,
143*795d594fSAndroid Build Coastguard Worker static_cast<uint16_t>(Instruction::GOTO | 0xFFFFFFFB << 8),
144*795d594fSAndroid Build Coastguard Worker Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 4,
145*795d594fSAndroid Build Coastguard Worker Instruction::RETURN_VOID);
146*795d594fSAndroid Build Coastguard Worker
147*795d594fSAndroid Build Coastguard Worker std::string expected_before =
148*795d594fSAndroid Build Coastguard Worker "BasicBlock 0, succ: 1\n"
149*795d594fSAndroid Build Coastguard Worker " 2: IntConstant [4]\n"
150*795d594fSAndroid Build Coastguard Worker " 3: IntConstant [4]\n"
151*795d594fSAndroid Build Coastguard Worker " 6: IntConstant [7]\n"
152*795d594fSAndroid Build Coastguard Worker " 9: IntConstant [10]\n"
153*795d594fSAndroid Build Coastguard Worker " 12: IntConstant [13]\n"
154*795d594fSAndroid Build Coastguard Worker " 0: SuspendCheck\n"
155*795d594fSAndroid Build Coastguard Worker " 1: Goto 1\n"
156*795d594fSAndroid Build Coastguard Worker "BasicBlock 1, pred: 0, succ: 3\n"
157*795d594fSAndroid Build Coastguard Worker " 4: Add(2, 3) [7]\n"
158*795d594fSAndroid Build Coastguard Worker " 5: Goto 3\n"
159*795d594fSAndroid Build Coastguard Worker "BasicBlock 2, pred: 3, succ: 4\n"
160*795d594fSAndroid Build Coastguard Worker " 10: Add(7, 9) [13]\n"
161*795d594fSAndroid Build Coastguard Worker " 11: Goto 4\n"
162*795d594fSAndroid Build Coastguard Worker "BasicBlock 3, pred: 1, succ: 2\n"
163*795d594fSAndroid Build Coastguard Worker " 7: Add(4, 6) [10]\n"
164*795d594fSAndroid Build Coastguard Worker " 8: Goto 2\n"
165*795d594fSAndroid Build Coastguard Worker "BasicBlock 4, pred: 2, succ: 5\n"
166*795d594fSAndroid Build Coastguard Worker " 13: Add(10, 12)\n"
167*795d594fSAndroid Build Coastguard Worker " 14: ReturnVoid\n"
168*795d594fSAndroid Build Coastguard Worker "BasicBlock 5, pred: 4\n"
169*795d594fSAndroid Build Coastguard Worker " 15: Exit\n";
170*795d594fSAndroid Build Coastguard Worker
171*795d594fSAndroid Build Coastguard Worker std::string expected_after =
172*795d594fSAndroid Build Coastguard Worker "BasicBlock 0, succ: 1\n"
173*795d594fSAndroid Build Coastguard Worker " 0: SuspendCheck\n"
174*795d594fSAndroid Build Coastguard Worker " 1: Goto 1\n"
175*795d594fSAndroid Build Coastguard Worker "BasicBlock 1, pred: 0, succ: 5\n"
176*795d594fSAndroid Build Coastguard Worker " 14: ReturnVoid\n"
177*795d594fSAndroid Build Coastguard Worker "BasicBlock 5, pred: 1\n"
178*795d594fSAndroid Build Coastguard Worker " 15: Exit\n";
179*795d594fSAndroid Build Coastguard Worker
180*795d594fSAndroid Build Coastguard Worker TestCode(data, expected_before, expected_after);
181*795d594fSAndroid Build Coastguard Worker }
182*795d594fSAndroid Build Coastguard Worker
183*795d594fSAndroid Build Coastguard Worker } // namespace art
184