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 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 19 | #include "arch/x86/instruction_set_features_x86.h" |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 20 | #include "code_generator_x86.h" |
| 21 | #include "constant_folding.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 22 | #include "dead_code_elimination.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 23 | #include "driver/compiler_options.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 24 | #include "graph_checker.h" |
| 25 | #include "optimizing_unit_test.h" |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 26 | #include "pretty_printer.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 27 | |
| 28 | #include "gtest/gtest.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Fixture class for the constant folding and dce tests. |
| 34 | */ |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 35 | class ConstantFoldingTest : public CommonCompilerTest { |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 36 | public: |
| 37 | ConstantFoldingTest() : pool_(), allocator_(&pool_) { |
| 38 | graph_ = CreateGraph(&allocator_); |
| 39 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 40 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 41 | void TestCode(const uint16_t* data, |
| 42 | const std::string& expected_before, |
| 43 | const std::string& expected_after_cf, |
| 44 | const std::string& expected_after_dce, |
| 45 | std::function<void(HGraph*)> check_after_cf, |
| 46 | Primitive::Type return_type = Primitive::kPrimInt) { |
| 47 | graph_ = CreateCFG(&allocator_, data, return_type); |
| 48 | TestCodeOnReadyGraph(expected_before, |
| 49 | expected_after_cf, |
| 50 | expected_after_dce, |
| 51 | check_after_cf); |
| 52 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 53 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 54 | void TestCodeOnReadyGraph(const std::string& expected_before, |
| 55 | const std::string& expected_after_cf, |
| 56 | const std::string& expected_after_dce, |
| 57 | std::function<void(HGraph*)> check_after_cf) { |
| 58 | ASSERT_NE(graph_, nullptr); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 59 | TransformToSsa(graph_); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 60 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 61 | StringPrettyPrinter printer_before(graph_); |
| 62 | printer_before.VisitInsertionOrder(); |
| 63 | std::string actual_before = printer_before.str(); |
| 64 | EXPECT_EQ(expected_before, actual_before); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 65 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 66 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 67 | X86InstructionSetFeatures::FromCppDefines()); |
| 68 | x86::CodeGeneratorX86 codegenX86(graph_, *features_x86.get(), CompilerOptions()); |
| 69 | HConstantFolding(graph_).Run(); |
| 70 | SSAChecker ssa_checker_cf(graph_); |
| 71 | ssa_checker_cf.Run(); |
| 72 | ASSERT_TRUE(ssa_checker_cf.IsValid()); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 73 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 74 | StringPrettyPrinter printer_after_cf(graph_); |
| 75 | printer_after_cf.VisitInsertionOrder(); |
| 76 | std::string actual_after_cf = printer_after_cf.str(); |
| 77 | EXPECT_EQ(expected_after_cf, actual_after_cf); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 78 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 79 | check_after_cf(graph_); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 80 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 81 | HDeadCodeElimination(graph_).Run(); |
| 82 | SSAChecker ssa_checker_dce(graph_); |
| 83 | ssa_checker_dce.Run(); |
| 84 | ASSERT_TRUE(ssa_checker_dce.IsValid()); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 85 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 86 | StringPrettyPrinter printer_after_dce(graph_); |
| 87 | printer_after_dce.VisitInsertionOrder(); |
| 88 | std::string actual_after_dce = printer_after_dce.str(); |
| 89 | EXPECT_EQ(expected_after_dce, actual_after_dce); |
| 90 | } |
| 91 | |
| 92 | ArenaPool pool_; |
| 93 | ArenaAllocator allocator_; |
| 94 | HGraph* graph_; |
| 95 | }; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 96 | |
| 97 | /** |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 98 | * Tiny three-register program exercising int constant folding on negation. |
| 99 | * |
| 100 | * 16-bit |
| 101 | * offset |
| 102 | * ------ |
| 103 | * v0 <- 1 0. const/4 v0, #+1 |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 104 | * v1 <- -v0 1. neg-int v1, v0 |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 105 | * return v1 2. return v1 |
| 106 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 107 | TEST_F(ConstantFoldingTest, IntConstantFoldingNegation) { |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 108 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 109 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 110 | Instruction::NEG_INT | 1 << 8 | 0 << 12, |
| 111 | Instruction::RETURN | 1 << 8); |
| 112 | |
| 113 | std::string expected_before = |
| 114 | "BasicBlock 0, succ: 1\n" |
| 115 | " 2: IntConstant [5]\n" |
| 116 | " 10: SuspendCheck\n" |
| 117 | " 11: Goto 1\n" |
| 118 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 119 | " 5: Neg(2) [8]\n" |
| 120 | " 8: Return(5)\n" |
| 121 | "BasicBlock 2, pred: 1\n" |
| 122 | " 9: Exit\n"; |
| 123 | |
| 124 | // Expected difference after constant folding. |
| 125 | diff_t expected_cf_diff = { |
| 126 | { " 2: IntConstant [5]\n", " 2: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 127 | { " 10: SuspendCheck\n", " 10: SuspendCheck\n" |
| 128 | " 12: IntConstant [8]\n" }, |
| 129 | { " 5: Neg(2) [8]\n", removed }, |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 130 | { " 8: Return(5)\n", " 8: Return(12)\n" } |
| 131 | }; |
| 132 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
| 133 | |
| 134 | // Check the value of the computed constant. |
| 135 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 136 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 137 | ASSERT_TRUE(inst->IsIntConstant()); |
| 138 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), -1); |
| 139 | }; |
| 140 | |
| 141 | // Expected difference after dead code elimination. |
| 142 | diff_t expected_dce_diff = { |
| 143 | { " 2: IntConstant\n", removed }, |
| 144 | }; |
| 145 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
| 146 | |
| 147 | TestCode(data, |
| 148 | expected_before, |
| 149 | expected_after_cf, |
| 150 | expected_after_dce, |
| 151 | check_after_cf); |
| 152 | } |
| 153 | |
| 154 | /** |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 155 | * Tiny three-register program exercising long constant folding on negation. |
| 156 | * |
| 157 | * 16-bit |
| 158 | * offset |
| 159 | * ------ |
| 160 | * (v0, v1) <- 4294967296 0. const-wide v0 #+4294967296 |
| 161 | * (v2, v3) <- -(v0, v1) 1. neg-long v2, v0 |
| 162 | * return (v2, v3) 2. return-wide v2 |
| 163 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 164 | TEST_F(ConstantFoldingTest, LongConstantFoldingNegation) { |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 165 | const int64_t input = INT64_C(4294967296); // 2^32 |
| 166 | const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW. |
| 167 | const uint16_t word1 = High16Bits(Low32Bits(input)); |
| 168 | const uint16_t word2 = Low16Bits(High32Bits(input)); |
| 169 | const uint16_t word3 = High16Bits(High32Bits(input)); // MSW. |
| 170 | const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( |
| 171 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, |
| 172 | Instruction::NEG_LONG | 2 << 8 | 0 << 12, |
| 173 | Instruction::RETURN_WIDE | 2 << 8); |
| 174 | |
| 175 | std::string expected_before = |
| 176 | "BasicBlock 0, succ: 1\n" |
| 177 | " 4: LongConstant [7]\n" |
| 178 | " 12: SuspendCheck\n" |
| 179 | " 13: Goto 1\n" |
| 180 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 181 | " 7: Neg(4) [10]\n" |
| 182 | " 10: Return(7)\n" |
| 183 | "BasicBlock 2, pred: 1\n" |
| 184 | " 11: Exit\n"; |
| 185 | |
| 186 | // Expected difference after constant folding. |
| 187 | diff_t expected_cf_diff = { |
| 188 | { " 4: LongConstant [7]\n", " 4: LongConstant\n" }, |
| 189 | { " 12: SuspendCheck\n", " 12: SuspendCheck\n" |
| 190 | " 14: LongConstant [10]\n" }, |
| 191 | { " 7: Neg(4) [10]\n", removed }, |
| 192 | { " 10: Return(7)\n", " 10: Return(14)\n" } |
| 193 | }; |
| 194 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
| 195 | |
| 196 | // Check the value of the computed constant. |
| 197 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 198 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 199 | ASSERT_TRUE(inst->IsLongConstant()); |
| 200 | ASSERT_EQ(inst->AsLongConstant()->GetValue(), INT64_C(-4294967296)); |
| 201 | }; |
| 202 | |
| 203 | // Expected difference after dead code elimination. |
| 204 | diff_t expected_dce_diff = { |
| 205 | { " 4: LongConstant\n", removed }, |
| 206 | }; |
| 207 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
| 208 | |
| 209 | TestCode(data, |
| 210 | expected_before, |
| 211 | expected_after_cf, |
| 212 | expected_after_dce, |
| 213 | check_after_cf, |
| 214 | Primitive::kPrimLong); |
| 215 | } |
| 216 | |
| 217 | /** |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 218 | * Tiny three-register program exercising int constant folding on addition. |
| 219 | * |
| 220 | * 16-bit |
| 221 | * offset |
| 222 | * ------ |
| 223 | * v0 <- 1 0. const/4 v0, #+1 |
| 224 | * v1 <- 2 1. const/4 v1, #+2 |
| 225 | * v2 <- v0 + v1 2. add-int v2, v0, v1 |
| 226 | * return v2 4. return v2 |
| 227 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 228 | TEST_F(ConstantFoldingTest, IntConstantFoldingOnAddition1) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 229 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
| 230 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 231 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
| 232 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 233 | Instruction::RETURN | 2 << 8); |
| 234 | |
| 235 | std::string expected_before = |
| 236 | "BasicBlock 0, succ: 1\n" |
| 237 | " 3: IntConstant [9]\n" |
| 238 | " 5: IntConstant [9]\n" |
| 239 | " 14: SuspendCheck\n" |
| 240 | " 15: Goto 1\n" |
| 241 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 242 | " 9: Add(3, 5) [12]\n" |
| 243 | " 12: Return(9)\n" |
| 244 | "BasicBlock 2, pred: 1\n" |
| 245 | " 13: Exit\n"; |
| 246 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 247 | // Expected difference after constant folding. |
| 248 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 249 | { " 3: IntConstant [9]\n", " 3: IntConstant\n" }, |
| 250 | { " 5: IntConstant [9]\n", " 5: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 251 | { " 14: SuspendCheck\n", " 14: SuspendCheck\n" |
| 252 | " 16: IntConstant [12]\n" }, |
| 253 | { " 9: Add(3, 5) [12]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 254 | { " 12: Return(9)\n", " 12: Return(16)\n" } |
| 255 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 256 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 257 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 258 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 259 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 260 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 261 | ASSERT_TRUE(inst->IsIntConstant()); |
| 262 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), 3); |
| 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 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 270 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 271 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 272 | TestCode(data, |
| 273 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 274 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 275 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 276 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Small three-register program exercising int constant folding on addition. |
| 281 | * |
| 282 | * 16-bit |
| 283 | * offset |
| 284 | * ------ |
| 285 | * v0 <- 1 0. const/4 v0, #+1 |
| 286 | * v1 <- 2 1. const/4 v1, #+2 |
| 287 | * v0 <- v0 + v1 2. add-int/2addr v0, v1 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 288 | * v1 <- 4 3. const/4 v1, #+4 |
| 289 | * v2 <- 5 4. const/4 v2, #+5 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 290 | * v1 <- v1 + v2 5. add-int/2addr v1, v2 |
| 291 | * v2 <- v0 + v1 6. add-int v2, v0, v1 |
| 292 | * return v2 8. return v2 |
| 293 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 294 | TEST_F(ConstantFoldingTest, IntConstantFoldingOnAddition2) { |
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 | 1 << 12, |
| 297 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
| 298 | Instruction::ADD_INT_2ADDR | 0 << 8 | 1 << 12, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 299 | Instruction::CONST_4 | 1 << 8 | 4 << 12, |
| 300 | Instruction::CONST_4 | 2 << 8 | 5 << 12, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 301 | Instruction::ADD_INT_2ADDR | 1 << 8 | 2 << 12, |
| 302 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 303 | Instruction::RETURN | 2 << 8); |
| 304 | |
| 305 | std::string expected_before = |
| 306 | "BasicBlock 0, succ: 1\n" |
| 307 | " 3: IntConstant [9]\n" |
| 308 | " 5: IntConstant [9]\n" |
| 309 | " 11: IntConstant [17]\n" |
| 310 | " 13: IntConstant [17]\n" |
| 311 | " 26: SuspendCheck\n" |
| 312 | " 27: Goto 1\n" |
| 313 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 314 | " 9: Add(3, 5) [21]\n" |
| 315 | " 17: Add(11, 13) [21]\n" |
| 316 | " 21: Add(9, 17) [24]\n" |
| 317 | " 24: Return(21)\n" |
| 318 | "BasicBlock 2, pred: 1\n" |
| 319 | " 25: Exit\n"; |
| 320 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 321 | // Expected difference after constant folding. |
| 322 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 323 | { " 3: IntConstant [9]\n", " 3: IntConstant\n" }, |
| 324 | { " 5: IntConstant [9]\n", " 5: IntConstant\n" }, |
| 325 | { " 11: IntConstant [17]\n", " 11: IntConstant\n" }, |
| 326 | { " 13: IntConstant [17]\n", " 13: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 327 | { " 26: SuspendCheck\n", " 26: SuspendCheck\n" |
| 328 | " 28: IntConstant\n" |
| 329 | " 29: IntConstant\n" |
| 330 | " 30: IntConstant [24]\n" }, |
| 331 | { " 9: Add(3, 5) [21]\n", removed }, |
| 332 | { " 17: Add(11, 13) [21]\n", removed }, |
| 333 | { " 21: Add(9, 17) [24]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 334 | { " 24: Return(21)\n", " 24: Return(30)\n" } |
| 335 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 336 | std::string expected_after_cf = Patch(expected_before, expected_cf_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 | // Check the values of the computed constants. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 339 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 340 | HInstruction* inst1 = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 341 | ASSERT_TRUE(inst1->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 342 | ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 12); |
| 343 | HInstruction* inst2 = inst1->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 344 | ASSERT_TRUE(inst2->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 345 | ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 9); |
| 346 | HInstruction* inst3 = inst2->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 347 | ASSERT_TRUE(inst3->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 348 | ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 3); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 349 | }; |
| 350 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 351 | // Expected difference after dead code elimination. |
| 352 | diff_t expected_dce_diff = { |
| 353 | { " 3: IntConstant\n", removed }, |
| 354 | { " 5: IntConstant\n", removed }, |
| 355 | { " 11: IntConstant\n", removed }, |
| 356 | { " 13: IntConstant\n", removed }, |
| 357 | { " 28: IntConstant\n", removed }, |
| 358 | { " 29: IntConstant\n", removed } |
| 359 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 360 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 361 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 362 | TestCode(data, |
| 363 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 364 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 365 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 366 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Tiny three-register program exercising int constant folding on subtraction. |
| 371 | * |
| 372 | * 16-bit |
| 373 | * offset |
| 374 | * ------ |
| 375 | * v0 <- 3 0. const/4 v0, #+3 |
| 376 | * v1 <- 2 1. const/4 v1, #+2 |
| 377 | * v2 <- v0 - v1 2. sub-int v2, v0, v1 |
| 378 | * return v2 4. return v2 |
| 379 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 380 | TEST_F(ConstantFoldingTest, IntConstantFoldingOnSubtraction) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 381 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
| 382 | Instruction::CONST_4 | 0 << 8 | 3 << 12, |
| 383 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
| 384 | Instruction::SUB_INT | 2 << 8, 0 | 1 << 8, |
| 385 | Instruction::RETURN | 2 << 8); |
| 386 | |
| 387 | std::string expected_before = |
| 388 | "BasicBlock 0, succ: 1\n" |
| 389 | " 3: IntConstant [9]\n" |
| 390 | " 5: IntConstant [9]\n" |
| 391 | " 14: SuspendCheck\n" |
| 392 | " 15: Goto 1\n" |
| 393 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 394 | " 9: Sub(3, 5) [12]\n" |
| 395 | " 12: Return(9)\n" |
| 396 | "BasicBlock 2, pred: 1\n" |
| 397 | " 13: Exit\n"; |
| 398 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 399 | // Expected difference after constant folding. |
| 400 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 401 | { " 3: IntConstant [9]\n", " 3: IntConstant\n" }, |
| 402 | { " 5: IntConstant [9]\n", " 5: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 403 | { " 14: SuspendCheck\n", " 14: SuspendCheck\n" |
| 404 | " 16: IntConstant [12]\n" }, |
| 405 | { " 9: Sub(3, 5) [12]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 406 | { " 12: Return(9)\n", " 12: Return(16)\n" } |
| 407 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 408 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 409 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 410 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 411 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 412 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 413 | ASSERT_TRUE(inst->IsIntConstant()); |
| 414 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1); |
| 415 | }; |
| 416 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 417 | // Expected difference after dead code elimination. |
| 418 | diff_t expected_dce_diff = { |
| 419 | { " 3: IntConstant\n", removed }, |
| 420 | { " 5: IntConstant\n", removed } |
| 421 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 422 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 423 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 424 | TestCode(data, |
| 425 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 426 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 427 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 428 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 431 | /** |
| 432 | * Tiny three-register-pair program exercising long constant folding |
| 433 | * on addition. |
| 434 | * |
| 435 | * 16-bit |
| 436 | * offset |
| 437 | * ------ |
| 438 | * (v0, v1) <- 1 0. const-wide/16 v0, #+1 |
| 439 | * (v2, v3) <- 2 2. const-wide/16 v2, #+2 |
| 440 | * (v4, v5) <- |
| 441 | * (v0, v1) + (v1, v2) 4. add-long v4, v0, v2 |
| 442 | * return (v4, v5) 6. return-wide v4 |
| 443 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 444 | TEST_F(ConstantFoldingTest, LongConstantFoldingOnAddition) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 445 | const uint16_t data[] = SIX_REGISTERS_CODE_ITEM( |
| 446 | Instruction::CONST_WIDE_16 | 0 << 8, 1, |
| 447 | Instruction::CONST_WIDE_16 | 2 << 8, 2, |
| 448 | Instruction::ADD_LONG | 4 << 8, 0 | 2 << 8, |
| 449 | Instruction::RETURN_WIDE | 4 << 8); |
| 450 | |
| 451 | std::string expected_before = |
| 452 | "BasicBlock 0, succ: 1\n" |
| 453 | " 6: LongConstant [12]\n" |
| 454 | " 8: LongConstant [12]\n" |
| 455 | " 17: SuspendCheck\n" |
| 456 | " 18: Goto 1\n" |
| 457 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 458 | " 12: Add(6, 8) [15]\n" |
| 459 | " 15: Return(12)\n" |
| 460 | "BasicBlock 2, pred: 1\n" |
| 461 | " 16: Exit\n"; |
| 462 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 463 | // Expected difference after constant folding. |
| 464 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 465 | { " 6: LongConstant [12]\n", " 6: LongConstant\n" }, |
| 466 | { " 8: LongConstant [12]\n", " 8: LongConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 467 | { " 17: SuspendCheck\n", " 17: SuspendCheck\n" |
| 468 | " 19: LongConstant [15]\n" }, |
| 469 | { " 12: Add(6, 8) [15]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 470 | { " 15: Return(12)\n", " 15: Return(19)\n" } |
| 471 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 472 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 473 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 474 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 475 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 476 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 477 | ASSERT_TRUE(inst->IsLongConstant()); |
| 478 | ASSERT_EQ(inst->AsLongConstant()->GetValue(), 3); |
| 479 | }; |
| 480 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 481 | // Expected difference after dead code elimination. |
| 482 | diff_t expected_dce_diff = { |
| 483 | { " 6: LongConstant\n", removed }, |
| 484 | { " 8: LongConstant\n", removed } |
| 485 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 486 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 487 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 488 | TestCode(data, |
| 489 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 490 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 491 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 492 | check_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 493 | Primitive::kPrimLong); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Tiny three-register-pair program exercising long constant folding |
| 498 | * on subtraction. |
| 499 | * |
| 500 | * 16-bit |
| 501 | * offset |
| 502 | * ------ |
| 503 | * (v0, v1) <- 3 0. const-wide/16 v0, #+3 |
| 504 | * (v2, v3) <- 2 2. const-wide/16 v2, #+2 |
| 505 | * (v4, v5) <- |
| 506 | * (v0, v1) - (v1, v2) 4. sub-long v4, v0, v2 |
| 507 | * return (v4, v5) 6. return-wide v4 |
| 508 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 509 | TEST_F(ConstantFoldingTest, LongConstantFoldingOnSubtraction) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 510 | const uint16_t data[] = SIX_REGISTERS_CODE_ITEM( |
| 511 | Instruction::CONST_WIDE_16 | 0 << 8, 3, |
| 512 | Instruction::CONST_WIDE_16 | 2 << 8, 2, |
| 513 | Instruction::SUB_LONG | 4 << 8, 0 | 2 << 8, |
| 514 | Instruction::RETURN_WIDE | 4 << 8); |
| 515 | |
| 516 | std::string expected_before = |
| 517 | "BasicBlock 0, succ: 1\n" |
| 518 | " 6: LongConstant [12]\n" |
| 519 | " 8: LongConstant [12]\n" |
| 520 | " 17: SuspendCheck\n" |
| 521 | " 18: Goto 1\n" |
| 522 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 523 | " 12: Sub(6, 8) [15]\n" |
| 524 | " 15: Return(12)\n" |
| 525 | "BasicBlock 2, pred: 1\n" |
| 526 | " 16: Exit\n"; |
| 527 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 528 | // Expected difference after constant folding. |
| 529 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 530 | { " 6: LongConstant [12]\n", " 6: LongConstant\n" }, |
| 531 | { " 8: LongConstant [12]\n", " 8: LongConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 532 | { " 17: SuspendCheck\n", " 17: SuspendCheck\n" |
| 533 | " 19: LongConstant [15]\n" }, |
| 534 | { " 12: Sub(6, 8) [15]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 535 | { " 15: Return(12)\n", " 15: Return(19)\n" } |
| 536 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 537 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 538 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 539 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 540 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 541 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 542 | ASSERT_TRUE(inst->IsLongConstant()); |
| 543 | ASSERT_EQ(inst->AsLongConstant()->GetValue(), 1); |
| 544 | }; |
| 545 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 546 | // Expected difference after dead code elimination. |
| 547 | diff_t expected_dce_diff = { |
| 548 | { " 6: LongConstant\n", removed }, |
| 549 | { " 8: LongConstant\n", removed } |
| 550 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 551 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 552 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 553 | TestCode(data, |
| 554 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 555 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 556 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 557 | check_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 558 | Primitive::kPrimLong); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Three-register program with jumps leading to the creation of many |
| 563 | * blocks. |
| 564 | * |
| 565 | * The intent of this test is to ensure that all constant expressions |
| 566 | * are actually evaluated at compile-time, thanks to the reverse |
| 567 | * (forward) post-order traversal of the the dominator tree. |
| 568 | * |
| 569 | * 16-bit |
| 570 | * offset |
| 571 | * ------ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 572 | * v0 <- 1 0. const/4 v0, #+1 |
| 573 | * v1 <- 2 1. const/4 v1, #+2 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 574 | * v2 <- v0 + v1 2. add-int v2, v0, v1 |
| 575 | * goto L2 4. goto +4 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 576 | * L1: v1 <- v0 + 5 5. add-int/lit16 v1, v0, #+5 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 577 | * goto L3 7. goto +4 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 578 | * L2: v0 <- v2 + 4 8. add-int/lit16 v0, v2, #+4 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 579 | * goto L1 10. goto +(-5) |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 580 | * L3: v2 <- v1 + 8 11. add-int/lit16 v2, v1, #+8 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 581 | * return v2 13. return v2 |
| 582 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 583 | TEST_F(ConstantFoldingTest, IntConstantFoldingAndJumps) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 584 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 585 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 586 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 587 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 588 | Instruction::GOTO | 4 << 8, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 589 | Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 5, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 590 | Instruction::GOTO | 4 << 8, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 591 | Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 4, |
Andreas Gampe | 58554b7 | 2015-10-20 21:08:52 -0700 | [diff] [blame] | 592 | static_cast<uint16_t>(Instruction::GOTO | 0xFFFFFFFB << 8), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 593 | Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 8, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 594 | Instruction::RETURN | 2 << 8); |
| 595 | |
| 596 | std::string expected_before = |
| 597 | "BasicBlock 0, succ: 1\n" |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 598 | " 3: IntConstant [9]\n" // v0 <- 1 |
| 599 | " 5: IntConstant [9]\n" // v1 <- 2 |
| 600 | " 13: IntConstant [14]\n" // const 5 |
| 601 | " 18: IntConstant [19]\n" // const 4 |
| 602 | " 24: IntConstant [25]\n" // const 8 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 603 | " 30: SuspendCheck\n" |
| 604 | " 31: Goto 1\n" |
| 605 | "BasicBlock 1, pred: 0, succ: 3\n" |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 606 | " 9: Add(3, 5) [19]\n" // v2 <- v0 + v1 = 1 + 2 = 3 |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 607 | " 11: Goto 3\n" // goto L2 |
| 608 | "BasicBlock 2, pred: 3, succ: 4\n" // L1: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 609 | " 14: Add(19, 13) [25]\n" // v1 <- v0 + 3 = 7 + 5 = 12 |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 610 | " 16: Goto 4\n" // goto L3 |
| 611 | "BasicBlock 3, pred: 1, succ: 2\n" // L2: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 612 | " 19: Add(9, 18) [14]\n" // v0 <- v2 + 2 = 3 + 4 = 7 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 613 | " 21: SuspendCheck\n" |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 614 | " 22: Goto 2\n" // goto L1 |
| 615 | "BasicBlock 4, pred: 2, succ: 5\n" // L3: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 616 | " 25: Add(14, 24) [28]\n" // v2 <- v1 + 4 = 12 + 8 = 20 |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 617 | " 28: Return(25)\n" // return v2 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 618 | "BasicBlock 5, pred: 4\n" |
| 619 | " 29: Exit\n"; |
| 620 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 621 | // Expected difference after constant folding. |
| 622 | diff_t expected_cf_diff = { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 623 | { " 3: IntConstant [9]\n", " 3: IntConstant\n" }, |
| 624 | { " 5: IntConstant [9]\n", " 5: IntConstant []\n" }, |
| 625 | { " 13: IntConstant [14]\n", " 13: IntConstant\n" }, |
| 626 | { " 18: IntConstant [19]\n", " 18: IntConstant\n" }, |
| 627 | { " 24: IntConstant [25]\n", " 24: IntConstant\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 628 | { " 30: SuspendCheck\n", " 30: SuspendCheck\n" |
| 629 | " 32: IntConstant []\n" |
| 630 | " 33: IntConstant []\n" |
| 631 | " 34: IntConstant\n" |
| 632 | " 35: IntConstant [28]\n" }, |
| 633 | { " 9: Add(3, 5) [19]\n", removed }, |
| 634 | { " 14: Add(19, 13) [25]\n", removed }, |
| 635 | { " 19: Add(9, 18) [14]\n", removed }, |
| 636 | { " 25: Add(14, 24) [28]\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 637 | { " 28: Return(25)\n", " 28: Return(35)\n"} |
| 638 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 639 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 640 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 641 | // Check the values of the computed constants. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 642 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 643 | HInstruction* inst1 = graph->GetBlocks()[4]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 644 | ASSERT_TRUE(inst1->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 645 | ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 20); |
| 646 | HInstruction* inst2 = inst1->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 647 | ASSERT_TRUE(inst2->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 648 | ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 12); |
| 649 | HInstruction* inst3 = inst2->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 650 | ASSERT_TRUE(inst3->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 651 | ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 7); |
| 652 | HInstruction* inst4 = inst3->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 653 | ASSERT_TRUE(inst4->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 654 | ASSERT_EQ(inst4->AsIntConstant()->GetValue(), 3); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 655 | }; |
| 656 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 657 | // Expected difference after dead code elimination. |
David Brazdil | 1c533c1 | 2015-04-24 17:04:38 +0100 | [diff] [blame] | 658 | std::string expected_after_dce = |
| 659 | "BasicBlock 0, succ: 1\n" |
| 660 | " 5: IntConstant []\n" |
| 661 | " 30: SuspendCheck\n" |
| 662 | " 32: IntConstant []\n" |
| 663 | " 33: IntConstant []\n" |
| 664 | " 35: IntConstant [28]\n" |
| 665 | " 31: Goto 1\n" |
| 666 | "BasicBlock 1, pred: 0, succ: 5\n" |
| 667 | " 21: SuspendCheck\n" |
| 668 | " 28: Return(35)\n" |
| 669 | "BasicBlock 5, pred: 1\n" |
| 670 | " 29: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 671 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 672 | TestCode(data, |
| 673 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 674 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 675 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 676 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 679 | /** |
| 680 | * Three-register program with a constant (static) condition. |
| 681 | * |
| 682 | * 16-bit |
| 683 | * offset |
| 684 | * ------ |
| 685 | * v1 <- 1 0. const/4 v1, #+1 |
| 686 | * v0 <- 0 1. const/4 v0, #+0 |
| 687 | * if v1 >= 0 goto L1 2. if-gez v1, +3 |
| 688 | * v0 <- v1 4. move v0, v1 |
| 689 | * L1: v2 <- v0 + v1 5. add-int v2, v0, v1 |
| 690 | * return-void 7. return |
| 691 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 692 | TEST_F(ConstantFoldingTest, ConstantCondition) { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 693 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
| 694 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 695 | Instruction::CONST_4 | 0 << 8 | 0 << 12, |
| 696 | Instruction::IF_GEZ | 1 << 8, 3, |
| 697 | Instruction::MOVE | 0 << 8 | 1 << 12, |
| 698 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 699 | Instruction::RETURN_VOID); |
| 700 | |
| 701 | std::string expected_before = |
| 702 | "BasicBlock 0, succ: 1\n" |
| 703 | " 3: IntConstant [15, 22, 8]\n" |
| 704 | " 5: IntConstant [22, 8]\n" |
| 705 | " 19: SuspendCheck\n" |
| 706 | " 20: Goto 1\n" |
| 707 | "BasicBlock 1, pred: 0, succ: 5, 2\n" |
| 708 | " 8: GreaterThanOrEqual(3, 5) [9]\n" |
| 709 | " 9: If(8)\n" |
| 710 | "BasicBlock 2, pred: 1, succ: 3\n" |
| 711 | " 12: Goto 3\n" |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 712 | "BasicBlock 3, pred: 5, 2, succ: 4\n" |
| 713 | " 22: Phi(5, 3) [15]\n" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 714 | " 15: Add(22, 3)\n" |
| 715 | " 17: ReturnVoid\n" |
| 716 | "BasicBlock 4, pred: 3\n" |
| 717 | " 18: Exit\n" |
| 718 | "BasicBlock 5, pred: 1, succ: 3\n" |
| 719 | " 21: Goto 3\n"; |
| 720 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 721 | // Expected difference after constant folding. |
| 722 | diff_t expected_cf_diff = { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 723 | { " 3: IntConstant [15, 22, 8]\n", " 3: IntConstant [9, 15, 22]\n" }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 724 | { " 5: IntConstant [22, 8]\n", " 5: IntConstant [22]\n" }, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 725 | { " 8: GreaterThanOrEqual(3, 5) [9]\n", removed }, |
| 726 | { " 9: If(8)\n", " 9: If(3)\n" } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 727 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 728 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 729 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 730 | // Check the values of the computed constants. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 731 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 732 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 733 | ASSERT_TRUE(inst->IsIntConstant()); |
| 734 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1); |
| 735 | }; |
| 736 | |
David Brazdil | 1c533c1 | 2015-04-24 17:04:38 +0100 | [diff] [blame] | 737 | // Expected graph after dead code elimination. |
| 738 | std::string expected_after_dce = |
| 739 | "BasicBlock 0, succ: 1\n" |
| 740 | " 19: SuspendCheck\n" |
| 741 | " 20: Goto 1\n" |
| 742 | "BasicBlock 1, pred: 0, succ: 4\n" |
| 743 | " 17: ReturnVoid\n" |
| 744 | "BasicBlock 4, pred: 1\n" |
| 745 | " 18: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 746 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 747 | TestCode(data, |
| 748 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 749 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 750 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 751 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 752 | } |
| 753 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 754 | /** |
| 755 | * Unsigned comparisons with zero. Since these instructions are not present |
| 756 | * in the bytecode, we need to set up the graph explicitly. |
| 757 | */ |
| 758 | TEST_F(ConstantFoldingTest, UnsignedComparisonsWithZero) { |
| 759 | graph_ = CreateGraph(&allocator_); |
| 760 | HBasicBlock* entry_block = new (&allocator_) HBasicBlock(graph_); |
| 761 | graph_->AddBlock(entry_block); |
| 762 | graph_->SetEntryBlock(entry_block); |
| 763 | HBasicBlock* block = new (&allocator_) HBasicBlock(graph_); |
| 764 | graph_->AddBlock(block); |
| 765 | HBasicBlock* exit_block = new (&allocator_) HBasicBlock(graph_); |
| 766 | graph_->AddBlock(exit_block); |
| 767 | graph_->SetExitBlock(exit_block); |
| 768 | entry_block->AddSuccessor(block); |
| 769 | block->AddSuccessor(exit_block); |
| 770 | |
| 771 | // Make various unsigned comparisons with zero against a parameter. |
| 772 | HInstruction* parameter = new (&allocator_) HParameterValue( |
| 773 | graph_->GetDexFile(), 0, 0, Primitive::kPrimInt, true); |
| 774 | entry_block->AddInstruction(parameter); |
| 775 | HInstruction* zero = graph_->GetIntConstant(0); |
| 776 | HInstruction* last; |
| 777 | block->AddInstruction(last = new (&allocator_) HAbove(zero, parameter)); |
| 778 | block->AddInstruction(new (&allocator_) HDeoptimize(last, 0)); |
| 779 | block->AddInstruction(last = new (&allocator_) HAbove(parameter, zero)); |
| 780 | block->AddInstruction(new (&allocator_) HDeoptimize(last, 0)); |
| 781 | block->AddInstruction(last = new (&allocator_) HAboveOrEqual(zero, parameter)); |
| 782 | block->AddInstruction(new (&allocator_) HDeoptimize(last, 0)); |
| 783 | block->AddInstruction(last = new (&allocator_) HAboveOrEqual(parameter, zero)); |
| 784 | block->AddInstruction(new (&allocator_) HDeoptimize(last, 0)); |
| 785 | block->AddInstruction(last = new (&allocator_) HBelow(zero, parameter)); |
| 786 | block->AddInstruction(new (&allocator_) HDeoptimize(last, 0)); |
| 787 | block->AddInstruction(last = new (&allocator_) HBelow(parameter, zero)); |
| 788 | block->AddInstruction(new (&allocator_) HDeoptimize(last, 0)); |
| 789 | block->AddInstruction(last = new (&allocator_) HBelowOrEqual(zero, parameter)); |
| 790 | block->AddInstruction(new (&allocator_) HDeoptimize(last, 0)); |
| 791 | block->AddInstruction(last = new (&allocator_) HBelowOrEqual(parameter, zero)); |
| 792 | block->AddInstruction(new (&allocator_) HDeoptimize(last, 0)); |
| 793 | |
| 794 | entry_block->AddInstruction(new (&allocator_) HGoto()); |
| 795 | block->AddInstruction(new (&allocator_) HReturn(zero)); |
| 796 | exit_block->AddInstruction(new (&allocator_) HExit()); |
| 797 | |
| 798 | const std::string expected_before = |
| 799 | "BasicBlock 0, succ: 1\n" |
| 800 | " 0: ParameterValue [16, 14, 12, 10, 8, 6, 4, 2]\n" |
| 801 | " 1: IntConstant [19, 16, 14, 12, 10, 8, 6, 4, 2]\n" |
| 802 | " 18: Goto 1\n" |
| 803 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 804 | " 2: Above(1, 0) [3]\n" |
| 805 | " 3: Deoptimize(2)\n" |
| 806 | " 4: Above(0, 1) [5]\n" |
| 807 | " 5: Deoptimize(4)\n" |
| 808 | " 6: AboveOrEqual(1, 0) [7]\n" |
| 809 | " 7: Deoptimize(6)\n" |
| 810 | " 8: AboveOrEqual(0, 1) [9]\n" |
| 811 | " 9: Deoptimize(8)\n" |
| 812 | " 10: Below(1, 0) [11]\n" |
| 813 | " 11: Deoptimize(10)\n" |
| 814 | " 12: Below(0, 1) [13]\n" |
| 815 | " 13: Deoptimize(12)\n" |
| 816 | " 14: BelowOrEqual(1, 0) [15]\n" |
| 817 | " 15: Deoptimize(14)\n" |
| 818 | " 16: BelowOrEqual(0, 1) [17]\n" |
| 819 | " 17: Deoptimize(16)\n" |
| 820 | " 19: Return(1)\n" |
| 821 | "BasicBlock 2, pred: 1\n" |
| 822 | " 20: Exit\n"; |
| 823 | |
| 824 | const std::string expected_after_cf = |
| 825 | "BasicBlock 0, succ: 1\n" |
| 826 | " 0: ParameterValue [16, 10, 6, 4]\n" |
| 827 | " 1: IntConstant [13, 3, 19, 16, 10, 6, 4]\n" |
| 828 | " 21: IntConstant [15, 9]\n" |
| 829 | " 18: Goto 1\n" |
| 830 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 831 | " 3: Deoptimize(1)\n" |
| 832 | " 4: Above(0, 1) [5]\n" |
| 833 | " 5: Deoptimize(4)\n" |
| 834 | " 6: AboveOrEqual(1, 0) [7]\n" |
| 835 | " 7: Deoptimize(6)\n" |
| 836 | " 9: Deoptimize(21)\n" |
| 837 | " 10: Below(1, 0) [11]\n" |
| 838 | " 11: Deoptimize(10)\n" |
| 839 | " 13: Deoptimize(1)\n" |
| 840 | " 15: Deoptimize(21)\n" |
| 841 | " 16: BelowOrEqual(0, 1) [17]\n" |
| 842 | " 17: Deoptimize(16)\n" |
| 843 | " 19: Return(1)\n" |
| 844 | "BasicBlock 2, pred: 1\n" |
| 845 | " 20: Exit\n"; |
| 846 | |
| 847 | const std::string expected_after_dce = expected_after_cf; |
| 848 | |
| 849 | auto check_after_cf = [](HGraph* graph) { |
| 850 | CHECK(graph != nullptr); |
| 851 | }; |
| 852 | |
| 853 | TestCodeOnReadyGraph(expected_before, |
| 854 | expected_after_cf, |
| 855 | expected_after_dce, |
| 856 | check_after_cf); |
| 857 | } |
| 858 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 859 | } // namespace art |