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