Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1 | /* |
| 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 Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 17 | #include <functional> |
| 18 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 19 | #include "code_generator_x86.h" |
| 20 | #include "constant_folding.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 21 | #include "dead_code_elimination.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 22 | #include "driver/compiler_options.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 23 | #include "graph_checker.h" |
| 24 | #include "optimizing_unit_test.h" |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 25 | #include "pretty_printer.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 26 | |
| 27 | #include "gtest/gtest.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | static void TestCode(const uint16_t* data, |
| 32 | const std::string& expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 33 | const std::string& expected_after_cf, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 34 | const std::string& expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 35 | std::function<void(HGraph*)> check_after_cf, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 36 | Primitive::Type return_type = Primitive::kPrimInt) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 37 | ArenaPool pool; |
| 38 | ArenaAllocator allocator(&pool); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 39 | HGraph* graph = CreateCFG(&allocator, data, return_type); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 40 | ASSERT_NE(graph, nullptr); |
| 41 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 42 | graph->TryBuildingSsa(); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 43 | |
| 44 | StringPrettyPrinter printer_before(graph); |
| 45 | printer_before.VisitInsertionOrder(); |
| 46 | std::string actual_before = printer_before.str(); |
| 47 | ASSERT_EQ(expected_before, actual_before); |
| 48 | |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 49 | x86::CodeGeneratorX86 codegen(graph, CompilerOptions()); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 50 | HConstantFolding(graph).Run(); |
Nicolas Geoffray | 942a378 | 2014-12-17 17:10:47 +0000 | [diff] [blame] | 51 | SSAChecker ssa_checker_cf(&allocator, graph); |
| 52 | ssa_checker_cf.Run(); |
| 53 | ASSERT_TRUE(ssa_checker_cf.IsValid()); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 54 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 55 | StringPrettyPrinter printer_after_cf(graph); |
| 56 | printer_after_cf.VisitInsertionOrder(); |
| 57 | std::string actual_after_cf = printer_after_cf.str(); |
| 58 | ASSERT_EQ(expected_after_cf, actual_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 59 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 60 | check_after_cf(graph); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 61 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 62 | HDeadCodeElimination(graph).Run(); |
Nicolas Geoffray | 942a378 | 2014-12-17 17:10:47 +0000 | [diff] [blame] | 63 | SSAChecker ssa_checker_dce(&allocator, graph); |
| 64 | ssa_checker_dce.Run(); |
| 65 | ASSERT_TRUE(ssa_checker_dce.IsValid()); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 66 | |
| 67 | StringPrettyPrinter printer_after_dce(graph); |
| 68 | printer_after_dce.VisitInsertionOrder(); |
| 69 | std::string actual_after_dce = printer_after_dce.str(); |
| 70 | ASSERT_EQ(expected_after_dce, actual_after_dce); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 75 | * Tiny three-register program exercising int constant folding on negation. |
| 76 | * |
| 77 | * 16-bit |
| 78 | * offset |
| 79 | * ------ |
| 80 | * v0 <- 1 0. const/4 v0, #+1 |
| 81 | * v1 <- -v0 1. neg-int v0, v1 |
| 82 | * return v1 2. return v1 |
| 83 | */ |
| 84 | TEST(ConstantFolding, IntConstantFoldingNegation) { |
| 85 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 86 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 87 | Instruction::NEG_INT | 1 << 8 | 0 << 12, |
| 88 | Instruction::RETURN | 1 << 8); |
| 89 | |
| 90 | std::string expected_before = |
| 91 | "BasicBlock 0, succ: 1\n" |
| 92 | " 2: IntConstant [5]\n" |
| 93 | " 10: SuspendCheck\n" |
| 94 | " 11: Goto 1\n" |
| 95 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 96 | " 5: Neg(2) [8]\n" |
| 97 | " 8: Return(5)\n" |
| 98 | "BasicBlock 2, pred: 1\n" |
| 99 | " 9: Exit\n"; |
| 100 | |
| 101 | // Expected difference after constant folding. |
| 102 | diff_t expected_cf_diff = { |
| 103 | { " 2: IntConstant [5]\n", " 2: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 104 | { " 10: SuspendCheck\n", " 10: SuspendCheck\n" |
| 105 | " 12: IntConstant [8]\n" }, |
| 106 | { " 5: Neg(2) [8]\n", removed }, |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 107 | { " 8: Return(5)\n", " 8: Return(12)\n" } |
| 108 | }; |
| 109 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
| 110 | |
| 111 | // Check the value of the computed constant. |
| 112 | auto check_after_cf = [](HGraph* graph) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 113 | HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 114 | ASSERT_TRUE(inst->IsIntConstant()); |
| 115 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), -1); |
| 116 | }; |
| 117 | |
| 118 | // Expected difference after dead code elimination. |
| 119 | diff_t expected_dce_diff = { |
| 120 | { " 2: IntConstant\n", removed }, |
| 121 | }; |
| 122 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
| 123 | |
| 124 | TestCode(data, |
| 125 | expected_before, |
| 126 | expected_after_cf, |
| 127 | expected_after_dce, |
| 128 | check_after_cf); |
| 129 | } |
| 130 | |
| 131 | /** |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 132 | * Tiny three-register program exercising int constant folding on addition. |
| 133 | * |
| 134 | * 16-bit |
| 135 | * offset |
| 136 | * ------ |
| 137 | * v0 <- 1 0. const/4 v0, #+1 |
| 138 | * v1 <- 2 1. const/4 v1, #+2 |
| 139 | * v2 <- v0 + v1 2. add-int v2, v0, v1 |
| 140 | * return v2 4. return v2 |
| 141 | */ |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 142 | TEST(ConstantFolding, IntConstantFoldingOnAddition1) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 143 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
| 144 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 145 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
| 146 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 147 | Instruction::RETURN | 2 << 8); |
| 148 | |
| 149 | std::string expected_before = |
| 150 | "BasicBlock 0, succ: 1\n" |
| 151 | " 3: IntConstant [9]\n" |
| 152 | " 5: IntConstant [9]\n" |
| 153 | " 14: SuspendCheck\n" |
| 154 | " 15: Goto 1\n" |
| 155 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 156 | " 9: Add(3, 5) [12]\n" |
| 157 | " 12: Return(9)\n" |
| 158 | "BasicBlock 2, pred: 1\n" |
| 159 | " 13: Exit\n"; |
| 160 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 161 | // Expected difference after constant folding. |
| 162 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 163 | { " 3: IntConstant [9]\n", " 3: IntConstant\n" }, |
| 164 | { " 5: IntConstant [9]\n", " 5: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 165 | { " 14: SuspendCheck\n", " 14: SuspendCheck\n" |
| 166 | " 16: IntConstant [12]\n" }, |
| 167 | { " 9: Add(3, 5) [12]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 168 | { " 12: Return(9)\n", " 12: Return(16)\n" } |
| 169 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 170 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 171 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 172 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 173 | auto check_after_cf = [](HGraph* graph) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 174 | HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 175 | ASSERT_TRUE(inst->IsIntConstant()); |
| 176 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), 3); |
| 177 | }; |
| 178 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 179 | // Expected difference after dead code elimination. |
| 180 | diff_t expected_dce_diff = { |
| 181 | { " 3: IntConstant\n", removed }, |
| 182 | { " 5: IntConstant\n", removed } |
| 183 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 184 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 185 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 186 | TestCode(data, |
| 187 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 188 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 189 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 190 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Small three-register program exercising int constant folding on addition. |
| 195 | * |
| 196 | * 16-bit |
| 197 | * offset |
| 198 | * ------ |
| 199 | * v0 <- 1 0. const/4 v0, #+1 |
| 200 | * v1 <- 2 1. const/4 v1, #+2 |
| 201 | * v0 <- v0 + v1 2. add-int/2addr v0, v1 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 202 | * v1 <- 4 3. const/4 v1, #+4 |
| 203 | * v2 <- 5 4. const/4 v2, #+5 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 204 | * v1 <- v1 + v2 5. add-int/2addr v1, v2 |
| 205 | * v2 <- v0 + v1 6. add-int v2, v0, v1 |
| 206 | * return v2 8. return v2 |
| 207 | */ |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 208 | TEST(ConstantFolding, IntConstantFoldingOnAddition2) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 209 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
| 210 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 211 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
| 212 | Instruction::ADD_INT_2ADDR | 0 << 8 | 1 << 12, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 213 | Instruction::CONST_4 | 1 << 8 | 4 << 12, |
| 214 | Instruction::CONST_4 | 2 << 8 | 5 << 12, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 215 | Instruction::ADD_INT_2ADDR | 1 << 8 | 2 << 12, |
| 216 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 217 | Instruction::RETURN | 2 << 8); |
| 218 | |
| 219 | std::string expected_before = |
| 220 | "BasicBlock 0, succ: 1\n" |
| 221 | " 3: IntConstant [9]\n" |
| 222 | " 5: IntConstant [9]\n" |
| 223 | " 11: IntConstant [17]\n" |
| 224 | " 13: IntConstant [17]\n" |
| 225 | " 26: SuspendCheck\n" |
| 226 | " 27: Goto 1\n" |
| 227 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 228 | " 9: Add(3, 5) [21]\n" |
| 229 | " 17: Add(11, 13) [21]\n" |
| 230 | " 21: Add(9, 17) [24]\n" |
| 231 | " 24: Return(21)\n" |
| 232 | "BasicBlock 2, pred: 1\n" |
| 233 | " 25: Exit\n"; |
| 234 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 235 | // Expected difference after constant folding. |
| 236 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 237 | { " 3: IntConstant [9]\n", " 3: IntConstant\n" }, |
| 238 | { " 5: IntConstant [9]\n", " 5: IntConstant\n" }, |
| 239 | { " 11: IntConstant [17]\n", " 11: IntConstant\n" }, |
| 240 | { " 13: IntConstant [17]\n", " 13: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 241 | { " 26: SuspendCheck\n", " 26: SuspendCheck\n" |
| 242 | " 28: IntConstant\n" |
| 243 | " 29: IntConstant\n" |
| 244 | " 30: IntConstant [24]\n" }, |
| 245 | { " 9: Add(3, 5) [21]\n", removed }, |
| 246 | { " 17: Add(11, 13) [21]\n", removed }, |
| 247 | { " 21: Add(9, 17) [24]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 248 | { " 24: Return(21)\n", " 24: Return(30)\n" } |
| 249 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 250 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 251 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 252 | // Check the values of the computed constants. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 253 | auto check_after_cf = [](HGraph* graph) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 254 | HInstruction* inst1 = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 255 | ASSERT_TRUE(inst1->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 256 | ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 12); |
| 257 | HInstruction* inst2 = inst1->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 258 | ASSERT_TRUE(inst2->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 259 | ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 9); |
| 260 | HInstruction* inst3 = inst2->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 261 | ASSERT_TRUE(inst3->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 262 | ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 3); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 263 | }; |
| 264 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 265 | // Expected difference after dead code elimination. |
| 266 | diff_t expected_dce_diff = { |
| 267 | { " 3: IntConstant\n", removed }, |
| 268 | { " 5: IntConstant\n", removed }, |
| 269 | { " 11: IntConstant\n", removed }, |
| 270 | { " 13: IntConstant\n", removed }, |
| 271 | { " 28: IntConstant\n", removed }, |
| 272 | { " 29: IntConstant\n", removed } |
| 273 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 274 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 275 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 276 | TestCode(data, |
| 277 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 278 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 279 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 280 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Tiny three-register program exercising int constant folding on subtraction. |
| 285 | * |
| 286 | * 16-bit |
| 287 | * offset |
| 288 | * ------ |
| 289 | * v0 <- 3 0. const/4 v0, #+3 |
| 290 | * v1 <- 2 1. const/4 v1, #+2 |
| 291 | * v2 <- v0 - v1 2. sub-int v2, v0, v1 |
| 292 | * return v2 4. return v2 |
| 293 | */ |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 294 | TEST(ConstantFolding, IntConstantFoldingOnSubtraction) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 295 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
| 296 | Instruction::CONST_4 | 0 << 8 | 3 << 12, |
| 297 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
| 298 | Instruction::SUB_INT | 2 << 8, 0 | 1 << 8, |
| 299 | Instruction::RETURN | 2 << 8); |
| 300 | |
| 301 | std::string expected_before = |
| 302 | "BasicBlock 0, succ: 1\n" |
| 303 | " 3: IntConstant [9]\n" |
| 304 | " 5: IntConstant [9]\n" |
| 305 | " 14: SuspendCheck\n" |
| 306 | " 15: Goto 1\n" |
| 307 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 308 | " 9: Sub(3, 5) [12]\n" |
| 309 | " 12: Return(9)\n" |
| 310 | "BasicBlock 2, pred: 1\n" |
| 311 | " 13: Exit\n"; |
| 312 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 313 | // Expected difference after constant folding. |
| 314 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 315 | { " 3: IntConstant [9]\n", " 3: IntConstant\n" }, |
| 316 | { " 5: IntConstant [9]\n", " 5: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 317 | { " 14: SuspendCheck\n", " 14: SuspendCheck\n" |
| 318 | " 16: IntConstant [12]\n" }, |
| 319 | { " 9: Sub(3, 5) [12]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 320 | { " 12: Return(9)\n", " 12: Return(16)\n" } |
| 321 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 322 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 323 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 324 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 325 | auto check_after_cf = [](HGraph* graph) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 326 | HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 327 | ASSERT_TRUE(inst->IsIntConstant()); |
| 328 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1); |
| 329 | }; |
| 330 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 331 | // Expected difference after dead code elimination. |
| 332 | diff_t expected_dce_diff = { |
| 333 | { " 3: IntConstant\n", removed }, |
| 334 | { " 5: IntConstant\n", removed } |
| 335 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 336 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 337 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 338 | TestCode(data, |
| 339 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 340 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 341 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 342 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 345 | /** |
| 346 | * Tiny three-register-pair program exercising long constant folding |
| 347 | * on addition. |
| 348 | * |
| 349 | * 16-bit |
| 350 | * offset |
| 351 | * ------ |
| 352 | * (v0, v1) <- 1 0. const-wide/16 v0, #+1 |
| 353 | * (v2, v3) <- 2 2. const-wide/16 v2, #+2 |
| 354 | * (v4, v5) <- |
| 355 | * (v0, v1) + (v1, v2) 4. add-long v4, v0, v2 |
| 356 | * return (v4, v5) 6. return-wide v4 |
| 357 | */ |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 358 | TEST(ConstantFolding, LongConstantFoldingOnAddition) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 359 | const uint16_t data[] = SIX_REGISTERS_CODE_ITEM( |
| 360 | Instruction::CONST_WIDE_16 | 0 << 8, 1, |
| 361 | Instruction::CONST_WIDE_16 | 2 << 8, 2, |
| 362 | Instruction::ADD_LONG | 4 << 8, 0 | 2 << 8, |
| 363 | Instruction::RETURN_WIDE | 4 << 8); |
| 364 | |
| 365 | std::string expected_before = |
| 366 | "BasicBlock 0, succ: 1\n" |
| 367 | " 6: LongConstant [12]\n" |
| 368 | " 8: LongConstant [12]\n" |
| 369 | " 17: SuspendCheck\n" |
| 370 | " 18: Goto 1\n" |
| 371 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 372 | " 12: Add(6, 8) [15]\n" |
| 373 | " 15: Return(12)\n" |
| 374 | "BasicBlock 2, pred: 1\n" |
| 375 | " 16: Exit\n"; |
| 376 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 377 | // Expected difference after constant folding. |
| 378 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 379 | { " 6: LongConstant [12]\n", " 6: LongConstant\n" }, |
| 380 | { " 8: LongConstant [12]\n", " 8: LongConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 381 | { " 17: SuspendCheck\n", " 17: SuspendCheck\n" |
| 382 | " 19: LongConstant [15]\n" }, |
| 383 | { " 12: Add(6, 8) [15]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 384 | { " 15: Return(12)\n", " 15: Return(19)\n" } |
| 385 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 386 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 387 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 388 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 389 | auto check_after_cf = [](HGraph* graph) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 390 | HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 391 | ASSERT_TRUE(inst->IsLongConstant()); |
| 392 | ASSERT_EQ(inst->AsLongConstant()->GetValue(), 3); |
| 393 | }; |
| 394 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 395 | // Expected difference after dead code elimination. |
| 396 | diff_t expected_dce_diff = { |
| 397 | { " 6: LongConstant\n", removed }, |
| 398 | { " 8: LongConstant\n", removed } |
| 399 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 400 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 401 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 402 | TestCode(data, |
| 403 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 404 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 405 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 406 | check_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 407 | Primitive::kPrimLong); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Tiny three-register-pair program exercising long constant folding |
| 412 | * on subtraction. |
| 413 | * |
| 414 | * 16-bit |
| 415 | * offset |
| 416 | * ------ |
| 417 | * (v0, v1) <- 3 0. const-wide/16 v0, #+3 |
| 418 | * (v2, v3) <- 2 2. const-wide/16 v2, #+2 |
| 419 | * (v4, v5) <- |
| 420 | * (v0, v1) - (v1, v2) 4. sub-long v4, v0, v2 |
| 421 | * return (v4, v5) 6. return-wide v4 |
| 422 | */ |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 423 | TEST(ConstantFolding, LongConstantFoldingOnSubtraction) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 424 | const uint16_t data[] = SIX_REGISTERS_CODE_ITEM( |
| 425 | Instruction::CONST_WIDE_16 | 0 << 8, 3, |
| 426 | Instruction::CONST_WIDE_16 | 2 << 8, 2, |
| 427 | Instruction::SUB_LONG | 4 << 8, 0 | 2 << 8, |
| 428 | Instruction::RETURN_WIDE | 4 << 8); |
| 429 | |
| 430 | std::string expected_before = |
| 431 | "BasicBlock 0, succ: 1\n" |
| 432 | " 6: LongConstant [12]\n" |
| 433 | " 8: LongConstant [12]\n" |
| 434 | " 17: SuspendCheck\n" |
| 435 | " 18: Goto 1\n" |
| 436 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 437 | " 12: Sub(6, 8) [15]\n" |
| 438 | " 15: Return(12)\n" |
| 439 | "BasicBlock 2, pred: 1\n" |
| 440 | " 16: Exit\n"; |
| 441 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 442 | // Expected difference after constant folding. |
| 443 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 444 | { " 6: LongConstant [12]\n", " 6: LongConstant\n" }, |
| 445 | { " 8: LongConstant [12]\n", " 8: LongConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 446 | { " 17: SuspendCheck\n", " 17: SuspendCheck\n" |
| 447 | " 19: LongConstant [15]\n" }, |
| 448 | { " 12: Sub(6, 8) [15]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 449 | { " 15: Return(12)\n", " 15: Return(19)\n" } |
| 450 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 451 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 452 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 453 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 454 | auto check_after_cf = [](HGraph* graph) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 455 | HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 456 | ASSERT_TRUE(inst->IsLongConstant()); |
| 457 | ASSERT_EQ(inst->AsLongConstant()->GetValue(), 1); |
| 458 | }; |
| 459 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 460 | // Expected difference after dead code elimination. |
| 461 | diff_t expected_dce_diff = { |
| 462 | { " 6: LongConstant\n", removed }, |
| 463 | { " 8: LongConstant\n", removed } |
| 464 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 465 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 466 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 467 | TestCode(data, |
| 468 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 469 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 470 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 471 | check_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 472 | Primitive::kPrimLong); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Three-register program with jumps leading to the creation of many |
| 477 | * blocks. |
| 478 | * |
| 479 | * The intent of this test is to ensure that all constant expressions |
| 480 | * are actually evaluated at compile-time, thanks to the reverse |
| 481 | * (forward) post-order traversal of the the dominator tree. |
| 482 | * |
| 483 | * 16-bit |
| 484 | * offset |
| 485 | * ------ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 486 | * v0 <- 1 0. const/4 v0, #+1 |
| 487 | * v1 <- 2 1. const/4 v1, #+2 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 488 | * v2 <- v0 + v1 2. add-int v2, v0, v1 |
| 489 | * goto L2 4. goto +4 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 490 | * L1: v1 <- v0 + 5 5. add-int/lit16 v1, v0, #+5 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 491 | * goto L3 7. goto +4 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 492 | * L2: v0 <- v2 + 4 8. add-int/lit16 v0, v2, #+4 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 493 | * goto L1 10. goto +(-5) |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 494 | * L3: v2 <- v1 + 8 11. add-int/lit16 v2, v1, #+8 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 495 | * return v2 13. return v2 |
| 496 | */ |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 497 | TEST(ConstantFolding, IntConstantFoldingAndJumps) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 498 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 499 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 500 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 501 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 502 | Instruction::GOTO | 4 << 8, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 503 | Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 5, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 504 | Instruction::GOTO | 4 << 8, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 505 | Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 4, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 506 | static_cast<uint16_t>(Instruction::GOTO | -5 << 8), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 507 | Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 8, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 508 | Instruction::RETURN | 2 << 8); |
| 509 | |
| 510 | std::string expected_before = |
| 511 | "BasicBlock 0, succ: 1\n" |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 512 | " 3: IntConstant [9]\n" // v0 <- 1 |
| 513 | " 5: IntConstant [9]\n" // v1 <- 2 |
| 514 | " 13: IntConstant [14]\n" // const 5 |
| 515 | " 18: IntConstant [19]\n" // const 4 |
| 516 | " 24: IntConstant [25]\n" // const 8 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 517 | " 30: SuspendCheck\n" |
| 518 | " 31: Goto 1\n" |
| 519 | "BasicBlock 1, pred: 0, succ: 3\n" |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 520 | " 9: Add(3, 5) [19]\n" // v2 <- v0 + v1 = 1 + 2 = 3 |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 521 | " 11: Goto 3\n" // goto L2 |
| 522 | "BasicBlock 2, pred: 3, succ: 4\n" // L1: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 523 | " 14: Add(19, 13) [25]\n" // v1 <- v0 + 3 = 7 + 5 = 12 |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 524 | " 16: Goto 4\n" // goto L3 |
| 525 | "BasicBlock 3, pred: 1, succ: 2\n" // L2: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 526 | " 19: Add(9, 18) [14]\n" // v0 <- v2 + 2 = 3 + 4 = 7 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 527 | " 21: SuspendCheck\n" |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 528 | " 22: Goto 2\n" // goto L1 |
| 529 | "BasicBlock 4, pred: 2, succ: 5\n" // L3: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 530 | " 25: Add(14, 24) [28]\n" // v2 <- v1 + 4 = 12 + 8 = 20 |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 531 | " 28: Return(25)\n" // return v2 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 532 | "BasicBlock 5, pred: 4\n" |
| 533 | " 29: Exit\n"; |
| 534 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 535 | // Expected difference after constant folding. |
| 536 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 537 | { " 3: IntConstant [9]\n", " 3: IntConstant\n" }, |
| 538 | { " 5: IntConstant [9]\n", " 5: IntConstant []\n" }, |
| 539 | { " 13: IntConstant [14]\n", " 13: IntConstant\n" }, |
| 540 | { " 18: IntConstant [19]\n", " 18: IntConstant\n" }, |
| 541 | { " 24: IntConstant [25]\n", " 24: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 542 | { " 30: SuspendCheck\n", " 30: SuspendCheck\n" |
| 543 | " 32: IntConstant []\n" |
| 544 | " 33: IntConstant []\n" |
| 545 | " 34: IntConstant\n" |
| 546 | " 35: IntConstant [28]\n" }, |
| 547 | { " 9: Add(3, 5) [19]\n", removed }, |
| 548 | { " 14: Add(19, 13) [25]\n", removed }, |
| 549 | { " 19: Add(9, 18) [14]\n", removed }, |
| 550 | { " 25: Add(14, 24) [28]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 551 | { " 28: Return(25)\n", " 28: Return(35)\n"} |
| 552 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 553 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 554 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 555 | // Check the values of the computed constants. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 556 | auto check_after_cf = [](HGraph* graph) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 557 | HInstruction* inst1 = graph->GetBlock(4)->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 558 | ASSERT_TRUE(inst1->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 559 | ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 20); |
| 560 | HInstruction* inst2 = inst1->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 561 | ASSERT_TRUE(inst2->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 562 | ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 12); |
| 563 | HInstruction* inst3 = inst2->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 564 | ASSERT_TRUE(inst3->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 565 | ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 7); |
| 566 | HInstruction* inst4 = inst3->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 567 | ASSERT_TRUE(inst4->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 568 | ASSERT_EQ(inst4->AsIntConstant()->GetValue(), 3); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 569 | }; |
| 570 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 571 | // Expected difference after dead code elimination. |
| 572 | diff_t expected_dce_diff = { |
| 573 | { " 3: IntConstant\n", removed }, |
| 574 | { " 13: IntConstant\n", removed }, |
| 575 | { " 18: IntConstant\n", removed }, |
| 576 | { " 24: IntConstant\n", removed }, |
| 577 | { " 34: IntConstant\n", removed }, |
| 578 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 579 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 580 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 581 | TestCode(data, |
| 582 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 583 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 584 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 585 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | |
| 589 | /** |
| 590 | * Three-register program with a constant (static) condition. |
| 591 | * |
| 592 | * 16-bit |
| 593 | * offset |
| 594 | * ------ |
| 595 | * v1 <- 1 0. const/4 v1, #+1 |
| 596 | * v0 <- 0 1. const/4 v0, #+0 |
| 597 | * if v1 >= 0 goto L1 2. if-gez v1, +3 |
| 598 | * v0 <- v1 4. move v0, v1 |
| 599 | * L1: v2 <- v0 + v1 5. add-int v2, v0, v1 |
| 600 | * return-void 7. return |
| 601 | */ |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 602 | TEST(ConstantFolding, ConstantCondition) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 603 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
| 604 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 605 | Instruction::CONST_4 | 0 << 8 | 0 << 12, |
| 606 | Instruction::IF_GEZ | 1 << 8, 3, |
| 607 | Instruction::MOVE | 0 << 8 | 1 << 12, |
| 608 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 609 | Instruction::RETURN_VOID); |
| 610 | |
| 611 | std::string expected_before = |
| 612 | "BasicBlock 0, succ: 1\n" |
| 613 | " 3: IntConstant [15, 22, 8]\n" |
| 614 | " 5: IntConstant [22, 8]\n" |
| 615 | " 19: SuspendCheck\n" |
| 616 | " 20: Goto 1\n" |
| 617 | "BasicBlock 1, pred: 0, succ: 5, 2\n" |
| 618 | " 8: GreaterThanOrEqual(3, 5) [9]\n" |
| 619 | " 9: If(8)\n" |
| 620 | "BasicBlock 2, pred: 1, succ: 3\n" |
| 621 | " 12: Goto 3\n" |
| 622 | "BasicBlock 3, pred: 2, 5, succ: 4\n" |
| 623 | " 22: Phi(3, 5) [15]\n" |
| 624 | " 15: Add(22, 3)\n" |
| 625 | " 17: ReturnVoid\n" |
| 626 | "BasicBlock 4, pred: 3\n" |
| 627 | " 18: Exit\n" |
| 628 | "BasicBlock 5, pred: 1, succ: 3\n" |
| 629 | " 21: Goto 3\n"; |
| 630 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 631 | // Expected difference after constant folding. |
| 632 | diff_t expected_cf_diff = { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 633 | { " 3: IntConstant [15, 22, 8]\n", " 3: IntConstant [9, 15, 22]\n" }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 634 | { " 5: IntConstant [22, 8]\n", " 5: IntConstant [22]\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 635 | { " 8: GreaterThanOrEqual(3, 5) [9]\n", removed }, |
| 636 | { " 9: If(8)\n", " 9: If(3)\n" } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 637 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 638 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 639 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 640 | // Check the values of the computed constants. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 641 | auto check_after_cf = [](HGraph* graph) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 642 | HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 643 | ASSERT_TRUE(inst->IsIntConstant()); |
| 644 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1); |
| 645 | }; |
| 646 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 647 | // Expected difference after dead code elimination. |
| 648 | diff_t expected_dce_diff = { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 649 | { " 3: IntConstant [9, 15, 22]\n", " 3: IntConstant [9, 22]\n" }, |
| 650 | { " 22: Phi(3, 5) [15]\n", " 22: Phi(3, 5)\n" }, |
| 651 | { " 15: Add(22, 3)\n", removed } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 652 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 653 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 654 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 655 | TestCode(data, |
| 656 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 657 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 658 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 659 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | } // namespace art |